Applescript Keystroke "⌘+" gives an error - applescript

My applescript isn't working. I want a keystroke that does ⌘+ on my mac.
Here is my current code.
set abc to "+"
tell application "System Events"
keystroke command & abc
keystroke a
end tell
When I click play, I get the error
"System Events got an error: Can’t make {command, \"n\"} into type text." number -1700 from {command, "n"} to text.
Anyone know how to fix this?

The syntax to press modifier keys is
set abc to "+"
tell application "System Events"
keystroke abc using command down
end tell

Related

In MacOSX, can't get applescript+automator service working to send text from any application to "Sublime Text 3"

Want to send text from any application to "Sublime Text 3" and this is how I am thinking of doing it
Create an "automator" service
Set a shortcut for it
Call shortcut from app
I am able to create the "automator" service and it works fine when tested from within "automator" (by means of the "Get Text" action)
But, when I trigger shortcut from application, "Sublime Text" does not open new tab with the selected text (and does nothing)
This is how I am setting up the "automator" service
Service receives selected text in any application
Copy to Clipboard
Run this applescript
on run {input, parameters}
tell application "System Events"
set frontmost of process "Sublime Text" to true
tell application "System Events" to keystroke "n" using command down
tell application "System Events" to keystroke "v" using command down
end tell
end run
Appreciate the help, thanks
The following example AppleScript code works for me:
on run {input, parameters}
tell application "Sublime Text" to activate
delay 1
tell application "System Events"
keystroke "n" using command down
delay 0.5
keystroke "v" using command down
end tell
end run
Note that the value of the delay commands may or may not need to be adjusted for timing on your system.

Save ppt as pdf via applescript with Preview.app

I write script to convert xls to pdf with applescript.
Here is my code:
set the_file to (choose file)
tell application "Preview"
activate
open the_file
end tell
tell application "System Events"
tell process "Preview"
keystroke "p" with command down
tell front window
UI elements
end tell
end tell
end tell
When i run script, i have error in
keystroke "p" with command down
whats wrong?
You need the keyword using instead of the keyword with. So, the erroneous line should read:
keystroke "p" using command down

Run a command in a new tab in terminal

I'm trying to write a script that could run several commands in several tabs in the terminal.
I found a lot of informations about it, but it doesn't work as I want it to. So I probably need AppleScript.
• This code run a command in a new window:
tell app "Terminal"
do script "echo hello"
end tell
• And this one open a new tab
tell application "Terminal"
activate
tell application "System Events"
keystroke "t" using {command down}
end tell
end tell
But I didn't figured out how to "mix" them. Any idea ?
tell application "Terminal"
activate
tell application "System Events"
keystroke "t" using {command down}
end tell
do script "echo hello" in selected tab of the front window
end tell

XCode 5 behavior script generating permission error

This morning I thought I'd spend a few minutes to write an XCode 5 script for Prefereces->behaviors to hide panels to better focus on Code and storyboard. A few command keys: how long can that take.....
I used AppleScript, and it runs fine from the applescript editor. I then exported it as a script bundle and hooked it to a behavior within XCode. When I hit the key command, I get the permissions error:
Failed to launch script /Users/sss/Documents/pers/XCode Scripts/XCodeCodeFocus.scptd: The operation couldn’t be completed. Permission denied
The AppleScript is very basic: I've tried:
activate application "Xcode"
tell application "System Events" to keystroke "Y" using command down
tell application "System Events" to keystroke "0" using {command down, option down}
tell application "System Events" to keystroke "1" using command down
and (without the activate command in case that's causing havoc)
tell application "System Events" to keystroke "Y" using command down
tell application "System Events" to keystroke "0" using {command down, option down}
tell application "System Events" to keystroke "1" using command down
File permissions on the file are:
drwxr-xr-x# 3 sss staff 102 Oct 4 06:31 XCode Code Focus copy.scptd
The file is in my code directory, but I can't see why that would affect things.
Is this broken in XCode 5, or am I missing something....
many thanks!

Applescript to capture window

I am trying to create an applescript to capture a window. The keyboard shortcut is cmd+shift+4 then space. I am not able to use this in applescript.
My code :
tell application "system events"
keystroke "21, 49" using {command down, shift down}
end tell
It doesnt work. The problem with the scrip it using space bar. I need to hold cmd, shift & 4 and then press space bar.
Try:
tell application "System Events"
keystroke (ASCII character 36) using {command down}
delay 1
keystroke space
end tell
This Applescript might work better for you rather than using GUI scripting
it uses the screen capture command line. for more info look at the screencapture Man page
set fileName to do shell script "date \"+Screen Shot %Y-%m-%d at %H.%M.%S.png\""
tell application "System Events" to set thePath to POSIX path of desktop folder
do shell script "screencapture -W " & "\"" & thePath & "/" & fileName & "\""
If all you want is to get to that point then use this:
tell application "System Events"
key code 21 using {shift down, command down}
delay 0.1
key code 49
end tell

Resources