Keystroke Control + Key in Applescript

Discussion in 'Mac Programming' started by OpenLaszlo, Nov 20, 2011.

  1. macrumors member

    OpenLaszlo

    Joined:
    Jan 3, 2009
    #1
    Hi all,

    I'm trying to make an applescript that opens up Finder and then input the command Control (the modifier key) + "x". I've been reading some documentation and it seems the following code should work (for creating that key input):

    tell application "System Events"
    tell application process "Finder"
    keystroke "x" using control
    end tell
    end tell

    However, it doesn't really do anything. Im pretty sure the modifier input "control" is not begin registered. Does anyone have any advice, or know how to fix my problem?

    Thanks.
     
  2. macrumors member

    Mark FX

    Joined:
    Nov 18, 2011
    #2
    Keystroke Control + Key in Applescript

    Firstly make sure that you have enabled GUI scripting support on your Mac.
    To do this open System Preferences, and in the Universal Access panel, select the "Enable access for assistive devices" check box, at the bottom of the window, if you dont do this, no GUI scripting will work.

    Or you can use this script in Applescript Editor to switch it on for you, but you will need to enter a Administrator password.

    Code:
    tell application "AppleScript Utility"
        set GUI Scripting enabled to true
    end tell
    
    Secondly your script code is not correct, it needs to be something like this.

    Code:
    tell application "Finder" to activate
    tell application "System Events"
        keystroke "x" using control down
    end tell
    
    or this.

    Code:
    tell application "Finder" to activate
    tell application "System Events"
        tell process "Finder"
            keystroke "x" using control down
        end tell
    end tell
    

    I hope this helps.

    Regards Mark
     
  3. thread starter macrumors member

    OpenLaszlo

    Joined:
    Jan 3, 2009
    #3
    Hi Mark,

    Thanks for the input. The code works perfectly now. I was unaware that you had to specify the "down" option after control. In the examples I saw I thought that was an additional key, like pressing control + the down arrow. I see I was mistaken.

    Many thanks!
     
  4. macrumors member

    Mark FX

    Joined:
    Nov 18, 2011
    #4
    Keystroke Control + Key in Applescript

    Happy to help.

    If you need to use more than one modifier key, then put then put them in an applescript list like this.

    Code:
    keystroke "x" using {control down, shift down}
    Also you can enter whole lines of text like this.

    Code:
    keystroke "Hello World!"
    All the best.

    Regards Mark
     
  5. macrumors member

    gcoghill

    Joined:
    Sep 14, 2008
    Location:
    Ohio
    #5
    How would I go about doing both of the above? I want to use TextExpander to activate when I type the specific text "attached", and have the AppleScript type out the word "attached" (since TextExpander will remove it), and then invoke the keystroke Control-Z

    I can get each to work individually, but not together. Here is the code I am using:

    Code:
    tell application "System Events"
    keystroke "attached"
    keystroke "z" using control down
    end tell
    
     

Share This Page