Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 20, 2011, 01: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, 02: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, 02: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, 03: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, 03: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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Using Click in AppleScript rebello95 Mac Programming 3 Nov 8, 2011 06:09 AM
Use backslash in Applescript running a Bash Shell Script rebello95 Mac Programming 12 Nov 3, 2011 01:56 PM
App to control Pandora Desktop with control keys? kyle1320 MacBook Pro 2 Sep 4, 2011 12:48 PM
Can no longer zoom in holding Control key...with my Kensington mouse kryszrich OS X 1 Sep 4, 2008 11:40 AM
Change 2nd "enter" key on PB to a control key? iWillard OS X 7 Aug 28, 2005 10:14 AM


All times are GMT -5. The time now is 05:10 PM.

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

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