I am playing around with JavaScript for Automation on OS X Yosemite.
I am trying to open up a new tab in the terminal application. Here is what I've got so far:
var Terminal = Application('Terminal);
var Tab = Terminal.Tab;
// Activate the Terminal App, creates a new window if there isn't one already
Terminal.activate();
// This contains all the windows
Terminal.windows;
// This contains the first window
Terminal.windows.at(0) // alternatively, Terminal.windows[0]
// This contains the tabs in the first window
Terminal.windows.at(0).tabs
The Terminal.windows.at(0).tabs is essentially an array. It has a .push method. I assumed that I could use the following statement to add a tab to the window:
Terminal.windows.at(0).tabs.push(new Tab());
but it throws a very general error:
Error -10000: AppleEvent handler failed.
The documentation is severely lacking and I'm thinking that this JavaScript for automation thing was just a gimik to get JavaScript developers onboard.
Note: I've seen AppleScript solutions that essentially just tell the System Events Application to press Command + T to open up a new tab. That feels very hacky and makes Command + T hardcoded in there.
the following code works for chrome and safari, but not work for terminal, I am still figuring out the reason, see if this info helps.
chrome = Application("Google Chrome")
newTab = chrome.Tab()
chrome.windows[0].tabs.push(newTab)
see if the following work in your case:
var system = Application('System Events');
var terminal = Application('Terminal');
// tell application "Terminal" to activate
terminal.activate();
// tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
system.keystroke('t', {using: 'command down'});
You can emulate the shortcut for one new tab. Also need declare the target tab
tell application "System Events" to keystroke "t" using {command down}
Look the example with two or more tabs
teel application "Terminal"
do script "cd ~/ && ls" in tab 1 of front window
tell application "System Events" to keystroke "t" using {command down}
do script "cd ~/Applications && ls" in tab 2 of front window
end tell
Related
I have found the applescript code on internet, to start a terminal and launch Elasticsearch
the code is
tell app "Terminal"
do script "elasticsearch-5.5.0/bin/./elasticsearch"
end tell
It works fine,
now I want to add more stuff on it, I need to open 4 more new tab not new window just tab (command + T). and then run different command such as log tail command, start kibana like one after another in each tab.
I am new to applescript and got tired by searching samples and tutorials, can anyone suggest a solution or your idea to achieve the automation.
Terminal's scripting dictionary isn't very good — a lot of 'Apple Event Handler Failed' errors — but you can manage it with code like the following:
tell application "Terminal" to activate
my makeTab("ls -al")
my makeTab("top")
my makeTab("cd ~/Documents")
on makeTab(cmd)
tell application "System Events" to keystroke "t" using {command down}
tell application "Terminal"
do script cmd in last tab of front window
end tell
delay 0.2
end makeTab
Just put whatever commands you want run inside the makeTab() call.
Running: OSX 10.11.3
So I have the script below. It is a naive hack to build an html link string based on the selected text and current url of a Firefox window. It's fragile but works ok.
However if I have Tor Browser running in addition to Firefox and open the script in Script Editor, I see that the code has been changed to reference Tor Browser:
tell application "TorBrowser" to activate
Yikes! I know Tor Browser is based on Firefox – but it seems to have its own process ID (confirmed in Activity Monitor). Even if it didn't why and how is the code being changed? I've tested this numerous times:
Without Tor Browser running: code doesn't change
Based on its "modified date" it is not being changed after I close it in Script Editor. (i.e. save it at 11:10, leave the window open until 11:12)
Duplicate script. Launch one copy with Tor Browser running: script is changed. Quit Tor Browser and launch second copy: script is not changed. There is no prompt to save the changed file when closing it.
Does anyone know what is going on? This seems very bizarre. Is there a workaround?
tell application "Firefox" to activate
delay 0.3
tell application "System Events"
tell process "Firefox"
keystroke "c" using command down
delay 0.1
end tell
set theHeadline to the clipboard
delay 1
tell process "Firefox"
keystroke "l" using command down
delay 0.1
keystroke "c" using command down
delay 0.1
set theUrl to the clipboard
end tell
set tagStart to "<a href ="
set tagMiddle to "><b><u>"
set tagEnd to "</u></b></a>"
set tag to tagStart & "\"" & theUrl & "\"" & tagMiddle & theHeadline & tagEnd
set the clipboard to tag
end tell
From the info.plist files, the CFBundleExecutable of these applications is "firefox", and the CFBundleSignature is "MOZB", maybe this causes this issue.
To avoid this, use the bundle identifier of the application in your script:
tell application id "org.mozilla.firefox" to activate
-- the bundle identifier of the "Tor Browser" application is "org.mozilla.tor browser"
Since the name of the processes is the same, use this:
tell application "System Events"
tell (first process whose its bundle identifier is "org.mozilla.firefox")
keystroke "c" using command down
delay 0.1
end tell
end tell
First I would recommend a couple of simple changes to your script. When you're working with system events, there is no need to use tell process within the system events block.
tell application "Firefox" to activate
delay 0.3
tell application "System Events"
keystroke "c" using command down
delay .1
set theHeadline to the clipboard
keystroke "l" using command down
delay 0.1
keystroke "c" using command down
delay 0.1
set theUrl to the clipboard
end tell
set tagStart to "<a href ="
set tagMiddle to "><b><u>"
set tagEnd to "</u></b></a>"
set tag to tagStart & "\"" & theUrl & "\"" & tagMiddle & theHeadline & tagEnd
set the clipboard to tag
end tell
Back to your original question, your compiler (Script Editor) is trying to correct your mistake. It assumes that your are meaning Tor since it sees that running. You and I know you really mean FireFox, but it assumes you've made a mistake. The good news is once you compile it and save it as an app, this shouldn't be an issue. Just make sure when you compile and save that you don't have Tor running and I believe you should be fine.
Btw... You'll see similar behavior if you were to change FireFox to to FoxFire or something else. It will usually prompt you to select the application, then you'd have to navigate to FireFox.
please forgive formatting, writing this on my phone
I'm building an Applescript that will attach specific files when the attachment prompt appears in Safari.
I initially tried do shell script "open /Users/ea/Desktop/Guru/Deliverables/" but that just opens a new finder window. I need to know how to navigate to the correct folder once the prompt appears (see image below).
I'm sure it's simple, but I'm brand new to Applescript.
The GUI script solution would be:
tell application "System Events" to tell process "Safari"
tell window 1 to tell sheet 1
key code 5 using {shift down, command down} --shortcut for go to folder
tell sheet 1
set value of text field 1 to <POSIX path to targetfolder as text>
click button 1
end tell
end tell
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
In PCSX, (ps1 emulator), i'm trying to automate the steps to play an iso. So, i'm doing this:
set thepath to path to me
set thesecondpath to POSIX path of thepath
set thethirdpath to "Contents/PSX/ROMS/img.bin"
set thefourthpath to "/Contents/PSX/PCSX.app"
set thefifthpath to thesecondpath & thefourthpath
set theultimatepath to thesecondpath & thethirdpath
tell application thefifthpath
activate
tell application "System Events"
keystroke "i" using {command down}
keystroke theultimatepath
delay 1.0
tell process "PCSX"
click button "Go"
end tell
key code 53
end tell
end tell
Running from the AppleScript Editor won't work. I made it to work running from the App it creates. PCSX and the img.bin are inside the Generated Package.
after pressing command+i, it opens a "Go to the folder" dialog, and i need to click Go and then Open
But doing this way, it won't find the dialog box. What am i doing wrong?
If Go and Open are the default buttons, try:
tell application "System Events"
keystroke return
delay 2
keystroke return
end tell
Although I don't have PCX installed here is an example of how to click the Go button from Finder's Go to Folder command.
tell application "System Events"
tell process "Finder"
click button "Go" of window "Go to Folder"
end tell
end tell
The reason your script won’t work from AppleScript Editor is that the “me” in “path to me” is the application that ran the AppleScript. When you are running the AppleScript in AppleScript Editor, that means AppleScript Editor itself. When you saved your AppleScript as a script application and ran it, the path to me pointed to your script application, because it was running its own AppleScript.
Also, this is incorrect:
tell process "Finder"
click button "Go" of window "Go to Folder"
end tell
The “Go” button is not on the window “Go to Folder.” It’s on a sheet which is attached to a Finder window which has the name of whatever folder is currently being viewed. So you have to describe the button as being on sheet 1 of window 1:
tell application "System Events"
tell process "Finder"
click button "Go" of sheet 1 of window 1
end tell
end tell
… but keep in mind that in another app, a similar looking button on a sheet may be on sheet 1 of group 1 of group 2 of window 3. UI Scripting is complicated.