I am still learning the basics of AppleScript.
When I try to run a simple "Hello World" script like:
tell application "TextEdit"
activate
end tell
tell application "System Events"
keystroke "Hello World!"
key code 36
end tell
The first time I run it, it writes "Hello World!" in TextEdit as it should.
But the second time, it writes "Hello World!" in Script Editor.
From then on, it will only write in Script Editor.
Am I missing something obvious in the script?
Or is there something about OS X that I should be looking at?
Thanks in advance! Feel free to respond with long winded answers, Kbase articles, or other discussion threads that I may have missed.
tell application "TextEdit" to activate
tell application "System Events"
tell application process "TextEdit" to set frontmost to true
keystroke "Hello World!" & return
end tell
Try this group of code. I'll explain how it works below, using comments (--).
tell application "TextEdit"
-- Activate TextEdit and bring it to the foreground
activate
-- Create a new document and assign it to a variable called 'myDocument'
set myDocument to make new document
-- Activate the above mentioned new document
tell myDocument
-- Add text to the above mentioned new document
set its text to its text & "Hello, world!"
end tell
end tell
Related
I have a folder of images that gets updated from a camera taking pictures periodically throughout the day. I'm trying to write some applescript that will create a slideshow from a folder of images but also update as more are added without having to rerun the script. I started out trying to do quick look but couldn't get that working. Any ideas on how best to tackle this?
UPDATE
this is what I have hacked together so far:
tell application "Finder" to set the_folder to get folder (choose folder)
tell application "Finder" to open the_folder
tell application "System Events"
tell process "Finder"
key code 124
keystroke "a" using command down
keystroke " " using option down
end tell
end tell
I don't believe this works if I add photos behind the scenes though.
This is what I came up with and it works perfectly for what I need.
tell application "Finder" to set the_folder to get folder (choose folder)
tell application "Finder" to open the_folder
tell application "System Events"
tell process "Finder"
key code 124
keystroke "a" using command down
keystroke " " using option down
end tell
end tell
repeat while true
delay 60
tell application "System Events"
tell process "Finder"
keystroke "a" using command down
end tell
end tell
end repeat
A few caveats... since it is using quick look, you can't really do anything on the computer while this is running since it can't have any other app activate while it is running (quick look closes when this happens). Also the repeat section is required to get quick look to pickup the new additions to the directory without losing focus. Pretty nasty stuff, but I couldn't really find another easy way to do it!
I want to use AppleScript to bring an app to the front. If I run the following script
tell application "System Events"
tell process id 916
activate
end tell
end tell
the process doesn't come to front. Instead, only the active window of the currently front-most app loses focus, but that app stays in front.
Is it even possible to do this with a process ID rather than an application name? I have tried this on Mac OS X 10.6.8 and 10.7.5.
I am looking for a plain AppleScript solution. It should not use any shell commands or any other solution. I want to use the process ID number because I might have running multiple instances of the same application (in the same file location).
I have found the following solution:
tell application "System Events"
set myProcesses to every process whose unix id is myPocessID
repeat with myProcess in myProcesses
set the frontmost of myProcess to true
end repeat
end tell
Foo's answer works too:
tell application "System Events"
set frontmost of every process whose unix id is myProcessID to true
end tell
set processID to 432--currently firefox for me
tell application "System Events" to set a to file of 1st item of (processes whose unix id = processID)
activate application (a as alias as string)
This uses the path to the app file, which is apparently necessary (not just the name).
I have another answer which uses do shell script; I could add that if you want.
I'm pretty good with AppleScript in general, so I feel that this shouldn't be difficult for me to figure out with just a little nudge in the right direction. I have looked around the site and found some other methods for this that don't use AppleScript, but as AppleScript is mainly what I know, I would prefer to use it for this purpose.
So, anyway, what I would like to do is to see if a process is already running (activated) before it proceeds to do the things I want to do. This should help clarify what I am trying to do:
tell application "AppleWorks 6" to activate
tell application "System Events"
repeat until "AppleWorks 6" is in the processes
delay 3
end repeat
end tell
--do script stuff with AppleWorks
When I tried this it returned false, even though AppleWorks was running:
"AppleWorks 6" is in the processes
Any basic help would be greatly appreciated
If you are running a version of OS X later than Leopard, there is a running property you can leverage, so System Events aren't really needed.
tell application "AppleWorks 6" to activate
repeat
delay 0.2
if (running of application "AppleWorks 6") then exit repeat
end repeat
You want to check the "name" property of each process. Try this...
tell application "AppleWorks 6" to activate
tell application "System Events"
repeat until "AppleWorks 6" is in name of processes
delay 3
end repeat
end tell
--do script stuff with AppleWorks
I made a quick translator app in Applescript which takes the whatever you have on the clipboard and puts it into Google Translate. It works with basic A-Z and a few symbols. If the app does not recognize the character, it just writes "a". I need it to support non-Latin charecters, like Japanese and symbols. I tried adding the "as Unicode text" at the end of line 2, but that did not work.
get the clipboard
set translateMe to the clipboard as Unicode text
tell application "Safari"
activate
end tell
tell application "Safari"
activate
open location "https://translate.google.com"
end tell
delay 1
tell application "System Events"
keystroke translateMe
end tell
You could use the URL-Syntax like this:
tell application "Safari"
activate
open location "https://translate.google.com/#auto/en/" & (the clipboard)
end tell
I'm experimenting with Applescript for the first time, and am trying to build a script to setup my default layout of applications for developing at work. This involves placing applications across multiple Mission Control spaces. My problem at hand is simply moving about the spaces. I found in many posts similar to this that such action could be achieved with
tell application "System Events"
tell process "Finder"
keystroke "1" using control down
end tell
end tell
if the appropriate key binding was in place. I made the Preferences change so I could use control+1 to move to the first MC space. However, running the script doesn't do anything. This is the event log output:
tell application "System Events"
keystroke "1" using control down
end tell
No errors that I can see, but again: new to Applescript. I've tried many variations of this command including wrapping control down in curly braces and wrapping the call to "System Events" inside a call to "Finder" like this
tell application "Finder"
tell application "System Events"
keystroke "1" using control down
end tell
end tell
but the output is exactly the same with no shift in view. I think I'm missing something here...
According to this question's responses, I tried adding in a delay to make sure I wasn't stepping on my own feet with running the script with CMD-r but nothing happens. I hear the sound effect when you try to click out of an important focus window (if that makes any sense), it's a short beep. Am I talking to the applications improperly?
EDIT
Ok I got something working, but I'm a little confused why this is the case.
tell application "Finder"
activate
delay 0.2
tell application "System Events" to keystroke "a" using control down
end tell
This accomplishes what I need, but I have to change the key binding to a letter. I can replicate the error tone by pressing control+1 when Applescript Editor is active. I guess there's a shortcut for AE that uses the key combo. But why is that running when Finder is supposed to be active?
To answer your question, in your working code the difference is that you activate the Finder before you issue the keystroke command. Keystroke commands are always sent to the frontmost application so you must always make sure to activate an application first as you have done.
If control-1 didn't work then I suspect either some Finder command uses that combo or some other application uses that in a global context meaning it intercepts that command no matter which application is frontmost. Otherwise it should work for you.
Finally, I would remove your system events line of code from the Finder tell block of code. There's no reason the tell the Finder to tell system events to perform a command. Just put that line on its own after the "end tell" line.
Good luck.
I've just been working with this - I don't want to activate finder before triggering what should be a global shortcut. Keystroke was not working, however I tried key code and that indeed works:
on run {}
tell application "System Events"
key code {18} using {command down}
end tell
return "success"
end run
There is a list of key codes at this question: https://apple.stackexchange.com/questions/36943/how-do-i-automate-a-key-press-in-applescript
This should work:
tell application "Finder"
activate
end tell
delay 0.2
tell application "System Events"
key code 18 using {control down}
end tell
I found this tool to be a good alternative that does not have that issue.
Installation is as simple as:
brew install socsieng/tap/sendkeys
Usage:
sendkeys send --initial-delay 0 --delay 0.001 --characters 'Hello'
https://github.com/socsieng/sendkeys