Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

OpenLaszlo

macrumors member
Original poster
Jan 3, 2009
42
0
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.
 

Mark FX

macrumors regular
Nov 18, 2011
159
17
West Sussex, UK
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
 

OpenLaszlo

macrumors member
Original poster
Jan 3, 2009
42
0
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!
 

Mark FX

macrumors regular
Nov 18, 2011
159
17
West Sussex, UK
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
 

gcoghill

macrumors member
Sep 14, 2008
69
8
Ohio
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

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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.