I have 2 scripts 1st is bash and 2nd is AppleScript on my macOS 10.14 mojave. I am calling AppleScript from Bash and getting error. If I run AppleScript directly it will run without any issue.
If I run bash script to execute AppleScript I will get dialog "ERROR: Unable to find application identifier."
If I run AppleScript in Script Editor it will show correct (org.mozilla.firefox) application identifier with dialog.
Why its not showing application identifier when I run bash script to execute AppleScript.
Bash script :
#!/bin/bash
/usr/bin/osascript /Users/mymac/Desktop/tst.scpt
AppleScript :
property IDI : "NOTSET"
try
set IDI to bundle identifier of (info for (path to application "Firefox"))
display dialog " Identifier is " with title "Test" buttons {"Exit"} default answer IDI
end try
if IDI is "NOTSET" then
display dialog "ERROR: Unable to find application identifier." with title "Test" buttons {"Exit"}
return
end if
I cant put both script into one need them separately in scripts.
I want to open an org-mode file selected in the Finder, by double clicking on it. But since I use Emacs in daemon-mode, I want to use the emacsclient command for that.
Thus the primary idea was to wrap the command emacsclient -c posixPathToFile in an AppleScript App to open it.
tell application "Finder"
set fileAlias to the selection as alias
set fileName to name of fileAlias
set posixPath to POSIX path of fileAlias
end tell
-- tell application "Emacs" to activate
try
do shell script "/usr/local/bin/emacsclient -c " & quoted form of posixPath
end try
I know some set commands are not needed. Let's assume this script is saved as Xemacs.app and that I associate this app to always open .org file.
Using this App does not work by double-clicking on the file, but rather if I select the file in the Finder and then call the Xemacs.app independently. Why ? I'm not confident enough with AppleScript to figure out what happens.
So the workaround was to use the Automator service
on run {input, parameters}
set posixPath to POSIX path of input
tell application "iTerm" to do shell script "/usr/local/bin/emacsclient -c " & quoted form of posixPath
return input
end run
The service is saved as 'Open in Emacs'
Now selecting a file and right-clicking and callig Service > "Open in Emacs" works and opens the file.
What is wrong with the first approach ?
ok, I solved my issue. The problem comes from my misunderstanding of the difference between ScriptEditor and the Automator. If I use the Automator to create an App and use the former script instead of creating an App using the ScriptEditor, then it works as expected.
One can simplify the process by creating an App in Automator and running a shell script instead of wrapping the command in Ascript.
I've created an AppleScript that runs a bash script and launches an application
I've saved the AppleScript as an application
On my computer, the bash script runs and so does that application; however, on another computer the AppleScript can't open the application because its from an unidentified developer
Is there a way to have the AppleScript open the application even though it's from an unidentified developer? Oris there a way to open the application with a bash script?
I know how to open an app inside a bash script, but it runs into the same issue (unidentified developer error)
I'm not looking to change my system preferences. I'm looking for a way to do it programmatically so any user can run the AppleScript on their machine
It's OK if the user has to "right-click, open" the final AppleScript to make the Apple Script run, it just needs to open the application that script is opening up without having to "right-click, open" again
Here is my AppleScript code:
tell application "Finder" to get POSIX path of ((path to me as text) & "::")
set scriptPath to quoted form of result & "BrewInstall.command"
tell application "Finder" to get POSIX path of ((path to me as text) & "::")
set appPath to result & "OracleInstall.app"
tell application appPath
activate
end tell
tell application "Terminal"
activate
do script scriptPath
end tell
If you first remove the quarantine attribute, it should work:
do shell script "xattr -d com.apple.quarantine & appPath
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
This script worked every time with Snow Leopard.
tell application "Terminal"
activate
do script "cd web_sites/project" in front window # this line highlighted on error
do script "mate ." in front window
do script "rvm 1.8.7" in front window
do script "script/server" in front window
delay 4
do shell script "open -a Firefox http://localhost:3000"
end tell
With Lion I keep getting this error:
error "Terminal got an error: Can’t get window 1." number -1728 from window 1
Thanks.
I'm running Lion. I can execute the following with no errors. I do not get an error in Applescript in any of the following situations 1) the application is not running, 2) the app is running and a window is open, and 3) the app is running and the directory path is not valid. In case 3 the Terminal shows an error but applescript does not.
tell application "Terminal"
activate
do script "cd Development/Images" in front window -- this line highlighted on error
do script "ls -al" in front window
end tell
So your problem is something not related to this actual code.
It seems the problem is not the code but the speed at which the operating system is functioning. As i mentioned before, the code executed just fine in Snow Leopard. To compensate, after the upgrade to Lion, if i add another delay to give Terminal time to finish activating, and increase the delay before opening Firefox, everything works. My laptop is a MacBook Pro with 2.7 GHz Intel Core i7 processor.
tell application "Terminal"
activate
delay 1
do script "cd web_sites/project" in front window # this line highlighted on error
do script "mate ." in front window
do script "rvm 1.8.7" in front window
do script "script/server" in front window
delay 5
do shell script "open -a Firefox http://localhost:3000"
end tell
If you are just trying to get a script to run once, simply retrying may help. Execution speed is also a function of other demands on the system.