Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 20, 2011, 02:32 PM   #1
OpenLaszlo
macrumors member
 
Join Date: Jan 2009
Keystroke Control + Key in Applescript

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.
__________________
15" Macbook Pro, 2.4 GHz Intel Core 2 Duo, 2 GB RAM, 250GB HD
iPod Touch 4G 32GB
OpenLaszlo is offline   0 Reply With Quote
Old Nov 20, 2011, 03:35 PM   #2
Mark FX
macrumors member
 
Join Date: Nov 2011
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
Mark FX is offline   0 Reply With Quote
Old Nov 20, 2011, 03:56 PM   #3
OpenLaszlo
Thread Starter
macrumors member
 
Join Date: Jan 2009
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!
__________________
15" Macbook Pro, 2.4 GHz Intel Core 2 Duo, 2 GB RAM, 250GB HD
iPod Touch 4G 32GB
OpenLaszlo is offline   0 Reply With Quote
Old Nov 20, 2011, 04:26 PM   #4
Mark FX
macrumors member
 
Join Date: Nov 2011
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
Mark FX is offline   0 Reply With Quote
Old Nov 6, 2012, 04:38 PM   #5
gcoghill
macrumors member
 
Join Date: Sep 2008
Location: Ohio
Quote:
Originally Posted by Mark FX View Post
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
gcoghill is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
iOS 7.1 Key Click Volume Control magilla iPhone Tips, Help and Troubleshooting 0 Apr 19, 2014 02:04 PM
iOS 7 Control Center missing key networking option tazdevl iOS 7 6 Sep 22, 2013 10:24 AM
Control + Arrow key (how to move one word left/right?) altjx OS X 10.8 Mountain Lion 3 Sep 1, 2013 04:04 PM
Help, my Control key maps to my Command key pacman1176 Mac OS X 10.7 Lion 2 Jan 22, 2013 05:37 PM
Mission Control & Command-Control Key Swap Babyboi Windows, Linux & Others on the Mac 0 Aug 3, 2012 12:58 AM

Forum Jump

All times are GMT -5. The time now is 01:25 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC