Context
In Safari, there is a text field, in which, contains name of an image with extension .png
, and I have to remove that extension.
Solution
Write a script, in which, simulate Delete(⌫) key 4 times, if preceding characters are .png
, thereby removing the extension.
Problem
Here it says that should be done with key code 51
but that doesn't seem work. A Google search came up with other key codes such as 127.
Things to Fix my solution
What is the correct Key Code? Is there a way to find it out yourself like Accessibility Inspector for buttons, fields etc. I tried recording, doesn't work? Does Apple have a official list a of Key Codes?
For those interested, below is the script, nothing fancy:
# Checks and remove .png if exists
tell application "System Events"
set oldClipboard to the clipboard
repeat 4 times
key code 123 using {shift down} # Left Arrow key. Selects last 4 chars
end repeat
keystroke "c" using {command down}
set newClipboard to the clipboard as text
if newClipboard is equal to ".png" then
#keystroke (ASCII character 127) # Other "code", doesn't work
key code 51
delay 0.1
end if
key code 124 # Right Arrow key, works
set the clipboard to oldClipboard
end tell
set x to "something.dmg"
<newline>if x ends with ".dmg" then set x to characters 1 thru -5 of x as string