Apple Script that copies/pastes text is broken in Yosemite - macos

I made a simple AppleScript that copies the layer names from Photoshop and pastes them into Illustrator. It worked fine in Mountain Lion but now it doesn't work properly in Yosemite. It repeats 6 times, but it doesn't seem to activate Illustrator on the first run through. It does activate Illustrator the other 5 times it repeats. Here it is:
repeat 6 times
tell application "Adobe Photoshop CC 2014" to activate
tell application "System Events"
tell process "Photoshop"
keystroke "/" using command down
keystroke "c" using command down
keystroke tab
end tell
end tell
delay 0.3
tell application "Adobe Illustrator" to activate
tell application "System Events"
tell process "Illustrator"
keystroke "v" using command down
keystroke return
keystroke "-"
keystroke space
end tell
end tell
end repeat
end
Thanks for any help!

tell application "System Events" to repeat 6 times
tell process "Photoshop" to repeat until frontmost is true
set frontmost to true
delay 1
end repeat
keystroke "/" using command down
keystroke "c" using command down
keystroke tab
tell process "Illustrator" to repeat until frontmost is true
set frontmost to true
delay 1
end repeat
keystroke "v" using command down
keystroke return
keystroke "-"
keystroke space
end repeat

Related

macOS send keystroke to the active app periodically

I am trying to send a keystroke (command+ shift+ r) to a macOS (Mojave) app called "Dbeaver" every minute as long as DBeaver is the active app. I have tried the following with no effect.
tell application "System Events"
set activeApp to name of first application process whose frontmost is true
if "DBeaver" is in activeApp then
tell application "System Events" to keystroke "r" using {command down, shift down}
end if
end tell
The script works perfectly fine if its simple like the following:
activate application "DBeaver"
tell application "System Events" to keystroke "r" using {command down, shift down}
I do not have the application you referred to but I tested this following AppleScript code using TextEdit.app and it worked. Let me know if you run into any errors or issues
tell application "System Events"
repeat while (exists of application process "DBeaver")
set activeApp to name of first application process whose frontmost is true
if "DBeaver" is in activeApp then
tell its application process "DBeaver"
repeat while frontmost
keystroke "r" using {command down, shift down}
delay 60
end repeat
end tell
end if
end repeat
end tell
You want to avoid using something like a repeat loop, as that will block the app's user interface (for quitting or to just avoid the spinning wheel of death). A relatively easy way to repeat stuff like that is to make a stay-open application, and put your repeating code in the idle handler, which uses a timer - for example:
on idle
tell application "System Events"
set activeApp to name of first application process whose frontmost is true
if "DBeaver" is in activeApp then
tell application "System Events" to keystroke "r" using {command down, shift down}
end if
end tell
return 60 -- do it again in 60 seconds
end
The statements in the idle handler are run when the app is idle; the return value determines the number of seconds before the handler is run again.

Applescript keystroke fails

I have a problem with my application, I want AppleScript to type "date" in Terminal:
(
activate application "Terminal"
tell application "System Events"
keystroke "date"
keystroke return
end tell
)
It works, but if the language of my keyboard is set to Russian, my app types "####" instead of "date". How to make AppleScript always use the English keyboard mapping?
If you have to send a string to the cursor/insertion point, you can avoid the keystroke command by storing the string on the clipboard and then pasting it.
tell application "Terminal" to activate
set theString to "date"
set the clipboard to theString
delay 0.1
tell application "System Events"
tell process "Terminal"
tell menu bar item "Edit" of menu bar 1
click menu item "Paste" of menu 1
end tell
end tell
end tell
delay 0.1
You should also explore sending a command to the terminal window as a command. Tell a Terminal window to do script.
tell application "Terminal"
activate
set thisWindow to do script "echo 'hello world'" in window 1
do script "echo 'goodbye all'" in thisWindow
end tell
You didn't give enough details of what you're doing to know how best to solve your problem.

The keystroke command in applescript can't identify case

tell application "iTerm" to activate
delay 0.1
tell application "System Events" to tell process "iTerm"
keystroke "Lsj!"
end tell
I want know how can i get the right way to enter some character. keystroke command can't identify case.
This should work
set the clipboard to "Lsj!" as text
tell application "iTerm" to activate
delay 0.1
tell application "System Events" to tell process "iTerm"
keystroke (the clipboard)
end tell
wch1zpink's answer should work.
You can also write out the letters individually if needed
keystroke "l" using {shift down} -- uppercase
keystroke "sj!" -- lowercase
That should work
tell application "iTerm" to activate
delay 0.1
tell application "System Events" to tell process "iTerm"
key code 37 using {shift down}
key code 1
key code 38
end tell

AppleScript to Save Preview Document

I'm using a third party application to copy an image to the clipboard. I would then like to perform an AppleScript that opens Preview, creates a New Document which will then use the clipboard as the content, then save this to a specified location.
I'm 99% of the way there - just can't get the document to save. I'm on OS X 10.9.5.
Here's my script so far:
set the save_location to the quoted form of "/Users/freddy/Documents/test.png"
tell application "Preview"
activate
end tell
tell application "System Events"
tell process "Preview"
keystroke "n" using {command down}
end tell
end tell
tell application "Preview"
activate
save front document as "PNG" in POSIX file save_location
end tell
I can't find the correct syntax for saving a Preview document. It will be the only document open at the time.
Try the following - note the comments:
# Do not use `quoted form of` - only needed and useful with `do shell script`
set the save_location to "/Users/jdoe/Documents/test.png"
tell application "Preview"
activate
end tell
tell application "System Events"
tell process "Preview"
keystroke "n" using {command down}
end tell
end tell
tell application "Preview"
# Wait until the new window appears.
# Trying to save before the window has appeared will fail.
# Note: - This assumes that NO window was initially open.
# - The code should be made more robust to eventually time out.
repeat until (count of windows) > 0
delay 0.3
end repeat
activate
# Save the document; `as "<format>"` doesn't seem to work, but the
# format is *implied* by the filename suffix (extension).
save front document in POSIX file save_location
end tell
This script works to save an image in the clipboard to disk, using Preview in OS 10.11.6 on a 2010 Mac Mini:
--CONDITIONS: Finder's clipboard has an image in it
tell application "Preview"
launch
activate
end tell
delay 2 --tweak the delays to what works
tell application "System Events" --not as elegant as "tell application...to tell process", but clearer, chronologically
keystroke "n" using command down --Preview creates a new window.
delay 1
keystroke "v" using command down --Finder's clipboard image is pasted into Preview window.
end tell
delay 1
tell application "System Events"
set the clipboard to "Joe Blow" --for name of forthcoming Preview file
delay 1
keystroke "w" using command down --Forces prompt of Preview to save image.
delay 1
keystroke "v" using command down --Pastes new filename into Preview's save window.
delay 1
key code 36 --"Return" key saves the new file.
end tell
--RESULT: A new image exists in your default folder, named "Joe Blow.png" (or whatever extension)

Applescript keystroke wont work in BBEdit

tell application "BBEdit"
activate
tell window 1
repeat 100 times
repeat 5 times
key code 124
end repeat
--select insertion point after character 5
keystroke return
--keystroke key code 36
end repeat
end tell
end tell
gives error
error "BBEdit got an error: Can’t get keystroke \"
\" of window 1." number -1728 from keystroke "
" of window 1
All I want to do is emulate moving the cursor 5 spaces and press the return key lots and lots of time.
Seemed simple enough...
delay 0.2
tell application "BBEdit"
activate
tell window 1
repeat 100 times
repeat 5 times
tell application "System Events"
key code 124
end tell
end repeat
tell application "System Events"
keystroke return
end tell
end repeat
end tell
end tell

Resources