Launch Two Applications with Applescript - macos

I have been trying to launch two applications that I created with a single applescript file. (I am using OS X Lion)
The code I am using is:
tell application "app1" to activate
tell application "app2" to activate
Doing either one of these two tells works, but both together only launches the first application...
Does anyone know what I an doing wrong?
The first application is a C application compiled by xCode, and the second application is an AIR/Flash application created/published in Flash CS4
Edit
I've also tried
do shell script "my_path_here/app1"
do shell script "my_path2_here/app2"
which only launches the first application.
I've also tried:
do shell script "my_path_here/app1" & "; my_path2_here/app2"
which STILL only launches the first application.
Edit 2
I've discovered that this script will work:
do shell script "cd my_path_here/ ; open app1 ; cd my_path2_here/ ; open app2"
This accomplishes what I need done, but still leaves open the original problem, as this is a workaround and not a fix... This might shed more light on what the issue could be though.

I couldn't reproduce the issue, so can't tell you why it's happening, but (assuming there's a timing problem) introducing a delay between the application launches may resolve the problem. You'll need to experiment with the delay time.
tell application "app1" to activate
delay 10
tell application "app2" to activate

tell application "app1" to activate
end if
tell application "app2" to activate
end if
Try that and see how it works.

I've discovered that this script will work:
do shell script "cd my_path_here/ ; open app1 ; cd my_path2_here/ ; open app2"
This accomplishes what I need done, but still leaves open the original problem, as this is a workaround and not a fix... This might shed more light on what the issue could be though.

Related

Prevent macos automator Terminal ssh script from opening two windows

I use a small macOS Automator script to ssh into a remote host so I can add it to the Dock:
tell application "Terminal"
activate
do script ("ssh robot#example.com;")
end tell
When used the first time, I get the expected remote Terminal window, but also an extra local Terminal window. All subsequent uses only generate a single window to the remote host.
I've tried some small alternatives like:
set currentWindow to do script...
and:
set currentTab to do script...
with the same result. Is there any way to prevent the initial local Terminal window?
I found a post that has the answer:
tell application "Terminal"
if not application "Terminal" is running then launch
do script ("ssh robot#example.com;")
activate
end tell

The application “SomeApp.app” can’t be opened?

I have created an Application from AppleScript. Here is the code,
tell application "Terminal"
activate
set shell to do script "clear" in window 1
do script "python ~/Desktop/Prog/Prog.py" in shell
end tell
I am getting the error message The application Prog can’t be opened when running from the Applications folder. However it works fine in other folders. Even works fine in sub-folder in Applications folder. Only not working in Applications folder.
How to fix this?
Try: term> chmod uog+x SomeApp.app/Contents/MacOS/applet

Why this bug - applescript , automator & applescript editor

My applescript gave correct output when ran inside automator as service with no input but it gives error message when ran outside automator (safari, preview etc.)
It also gives correct output when ran through applescript editor, no problem running it with safari, preview or any other application.
The reason I was so keen to get it done with automator was that I don't want to install a 3rd party app just for assigning one shortcut to one script.
I debugged this portion which after removing from code allowed automator to run the script. Ran it with try, error in automator & viola, no issues at all.
But I want to know why this code earlier & without try-error function was giving the error messages when ran outside automator application.
Here's the portion of the script with try-error function:
try
set writ to do shell script "/usr/bin/python Users/[username]/Documents/tech_toolkit/windtitle"
tell application writ
if the (count of windows) is not 0 then
set window_title to name of front window
end if
end tell
on error
delay 2
end try
For shell script /windtitle , windtitle is an executable file
#!/usr/bin/env python
from AppKit import NSWorkspace
active_app_name = NSWorkspace.sharedWorkspace().frontmostApplication().localizedName()
print active_app_name
The error is:
Your python script return the "Automator Runner" application when it run as service , it's
the frontmost in the background but not in the foreground.
To get the frontmost application, use this script:
try
tell application (path to frontmost application as text)
set window_title to name of front window
set writ to its name -- get the name of this application
end tell
on error
return -- quit this script, because this application is not scriptable, or no window
end try

How can I launch an AppleScript application and run shell scripts from another AppleScript?

Here are my files:
launcher.applescript
tell application ":path:to:applescript:apps:shell-script-launcher.app" to launch
shell-script-launcher.app [AppleScript, saved as Application]
do shell script "say starting script"
Desired behavior:
Run "launcher.applescript" from AppleScript Editor
Listen for "starting script"
Actual behavior:
Running the "shell-script-launcher.app" by opening it manually in the finder yields expected behavior
Running the "launcher.applescript" opens the "shell-script-launcher.app" but it never executes the shell script.
I've tried saving the app as "Run Only" as well as "Stay Open". Still no progress. What do you recommend. The final result must be an Application instead of an Applescript.
Conceptually:
the run command launches and runs an application hidden
the activate command launches, runs, and activates the application (makes it the frontmost application)
launch, according to Apple, "Launches an application, if it is not already running, but does not send it a run command."
for AppleScript-based applications this should mean that they're loaded, but not executed (i.e., their - implicit or explicit - on run handler is NOT invoked), but in practice that is not true up to 10.9 - see below.
it is unclear (to me) what exactly that means for non-AppleScript-based applications
Here's how Apple thinks it works with AppleScript-based applications, which is only true starting with OSX 10.10 (Yosemite):
A script can send commands to a script application just as it can to other applications. To launch a non-stay-open application and run its script, use a launch command followed by a run command, like this:
launch application "NonStayOpen"
run application "NonStayOpen"
The launch command launches the script application without sending it an implicit run command. When the run command is sent to the script application, it processes the command, sends back a reply if necessary, and quits.
Broken behavior on OSX 10.8, 10.9 (fixed in OSX 10.10):
launch by itself is enough to run the application and is indeed the only command that works with AppleScript-based applications.
Any attempt to execute run or activate (whether in addition to launch or not) runs the application - even twice when run from AppleScript editor(!; just once with osascript) - but reports failure <appName> got an error: Connection is invalid.
This strikes me as bug.
Not sure how OSX versions <= 10.7 behave.
Note: I've witnessed the non-executing behavior with launch once, but every non-stay-open AppleScript-based test app I've created from scratch on OS X 10.9.2 and OS X 10.8.5 also executes the script with launch - contradicting what the documentation says.
Please let me know if your system behaves differently and/or how older versions behave. On what OSX version was the app that doesn't execute with launch created?
On OSX 10.10, behavior is consistent with the documentation, with one thing worth noting:
If the intent is to launch and run in one step, run application is sufficient - no need for a separate launch application command first.
Options
#user309603's pragmatic solution simply uses do shell script with the standard open utility to bypass the problem - this should work, regardless of whether the application is AppleScript-based or not:
do shell script "open " & ¬
quoted form of POSIX path of ¬
alias ":path:to:applescript:apps:shell-script-launcher.app"
If you know the type of application you're invoking up front:
to run an AppleScript-based app: best to use run script file, as #regulus6633 recommends - this has the added advantage that the invoked AppleScript-based application can return objects directly to the caller:
run script file ":path:to:applescript:apps:shell-script-launcher.app"
Note: There's also load script file, which indeed lets you merely load script code without executing it right away.
to run non-AppleScript apps: use run / activate to run the app hidden / frontmost:
run application ":path:to:applescript:apps:shell-script-launcher.app"
You could use run even with AppleScript-based applications and simply ignore errors with try ... end try, as #atonus suggests - the downside is that you won't be able to detect actual failure to invoke the application.
You can mitigate this by selectively ignoring only the specific Connection invalid error (which assumes this error would not legitimately occur) [no longer needed on OSX 10.10]:
try
run application "Macintosh HD:Applications:_Sandbox-AppleScript0.app"
on error number -609 # 'Connection is invalid' error that is spuriously reported
# simply ignore
end try
Finally, on OSX <= 10.9, you could try to simply use the launch command (though that didn't work for the OP, possibly due to working on a <= 10.7 OSX version):
launch application ":path:to:applescript:apps:shell-script-launcher.app"
However, that is not advisable for two reasons:
In OSX 10.10, Apple has fixed the launch behavior to no longer execute also, so your code will break when run there.
While non-AppleScript apps typically do run (hidden) when invoked with launch, the documentation says that AppleScript "does not send it a run command" and "allows you to open an application without performing its usual startup procedures, such as opening a new window" - what that exactly means is not clear and different applications seem to handle this differently.
Applescript has the "run script" command. It works on applescripts or applescript applications. So if I have your app on my desktop then this works...
set appPath to (path to desktop as text) & "shell-script-launcher.app"
run script file appPath
Have you tried open?
do shell script "open 'path/to/applescript/apps/shell-script-launcher.app' && say starting script"
Put it between "try" keywords.
try
tell application ":path:to:applescript:apps:shell-script-launcher.app" to activate
end try
Save your script as an application.
Add the desired script into the Script folder (see path below)
Run as follows, change per type of script you want to run...
property theApplicationPath : the path to me as text <br/>
property theShellScriptPath : theApplicationPath & "Contents:Resources:Scripts:test.command"<br/>
property theShellScript : the quoted form of POSIX path of theShellScriptPath<br/>
<br/>
tell application "Terminal" to (do shell script "/bin/bash " & theShellScript)
<br/>
Boom, Bob's your uncle! At least for me.
It is quite simple, you just have to use the normal do shell script, for example:
do shell script "open " & ¬
quoted form of POSIX path of ¬
alias ":path:to:applescript:apps:shell-script-launcher.app"
This may be helpful but I'm not an expert on this subject. I this case I found the double launching behaviour (on Yosemite, I haven't used any other version of OS X) very annoying and digging around I found one can check if it is already running.
This script is so I can launch a new terminal whenever I like.
if application "Terminal" is running then
tell application "Terminal"
activate
do script ""
end tell
else
tell application "Terminal" to activate
end if

Applescript to open application from CD Drive

I have a flash program on CD which i need to auto run when Cd insert in drive.
tell application "Finder"
activate
if exists disk "MYRIAD" then
tell application "Mac"
activate
end tell
end if
end tell
Meriad is my CD name and Mac is my flash application. it fire up the Mac application when i test it from desktop but when i burn it on CD it just open FINDER but nothing else.
Now i have also written code in my flash fla in second line to execute Mac application.
stop();
fscommand ("exec", "Mac");
fscommand("fullscreen", true);
fscommand("allowscale", true);
fscommand("showmenu", false);
i dont understand where i am going wrong.
please somebody help.... Thanks in advance.
There is a couple of ways you can do this.
But try this.
tell application "Finder"
activate
if exists disk "MYRIAD" then
tell application "MYRIAD:Mac.app" to activate
end if
end tell
Make sure you set the correct path in the "tell application "MYRIAD:Mac.app" to activate"
line.
separate each directory with a colon ":" but do not put one at beginning or end of the path to the app.
In my example the fist directory is the CD itself. The no other directories leading to the app.
Also make sure you name the app as it appears in the finder.

Resources