0

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
  • 1
    What's the bigger picture you're trying to achieve? There might be a safer way to achieve it than to simulate keystrokes. – Ilari Scheinin Oct 26 '15 at 16:33
  • 1
    ... along these lines: set x to "something.dmg" <newline> if x ends with ".dmg" then set x to characters 1 thru -5 of x as string – Ilari Scheinin Oct 26 '15 at 16:38
  • Far less of an XY Problem done like that @IlariScheinin ;) – Tetsujin Oct 26 '15 at 17:44
  • key code 51 DOES work as back delete. I just tested it out in TextEdit. What app and context do you have in front when this code of yours is running? Test with a beep or dialog before your key code 51 to make sure the problem is not with your code logic. – jweaks Oct 26 '15 at 20:15
  • @jweaks Added more info. I know script is running fine because I see the text get selected, but it never deletes it. – user14492 Oct 26 '15 at 21:34
0

Your script works fine in most apps. Can easily test it in TextEdit to see that. When I said add in dialog boxes to text the flow, mostly what you need to test is that the "if newClipboard is equal to ".png" is getting triggered (not if the text is getting selected. I would guess that your problem is that in a web form box (which I think you were describing), javascript is slowing down the text response including the copying to clipboard. It is a VERY common problem when scripting copying to clipboard that you need to add a delay after copying in order for the process to complete before the script proceeds. Try this, which simply adds the delay:

tell application "Safari" to activate
delay 1
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
    delay .5
    keystroke "c" using {command down}
    delay 2
    set newClipboard to the clipboard as text
    -- display dialog newClipboard
    if newClipboard is equal to ".png" then
        #keystroke # WORKS!!!!!!!!
        key code 51
        delay 0.1
    end if

    key code 124 # Right Arrow key, works
    set the clipboard to oldClipboard
end tell
  • Thanks for pointing out the lag in JS. I still couldn't get the key code 51 to work. I just keep the text selected, if it's equal and start to type, so it just replaces the text. – user14492 Nov 5 '15 at 16:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .