I'm trying to build an AppleScript to launch my shell script.
Path structure is as follows
/Users/ryan/myscript/
applescript.scpt
bash.sh
My AppleScript is as follows:
tell application "Terminal"
set folder_path to path to me
set run_cmd to "/bin/bash " & folder_path & "/bash.sh"
do script run_cmd
activate
end tell
Problem is the 'path to me' is not always returning the correct path. When executed using the Mac cd/dvd autoplay behavior folder_path is equal to:
disk:System:Library:CoreServices:SystemUIServer.app:Contents:XPCServices:com.apple.systemuiserver.scriptrunner.xpc:
Is there is a better way of getting the folder path?
If this Script is in a static location, you can do this:
do shell script "/bin/bash" & POSIX path of (path to current user folder) & "myscript/bash.sh"
Path to me refers to the location of the applescript that is running. So if your script is on a disk then it will reference the location on the disk where the script is saved
if it is expected that the shell script will always exist in a folder called "myscripts" that exists in the current user folder then you could use path to current user folder and build out from there
set user_folder to path to current user folder
set folder_path to quoted form of POSIX path of (("" & user_folder & "myscript"))
tell application "Terminal"
activate
set run_cmd to "/bin/bash " & folder_path & "/bash.sh"
do script run_cmd
end tell
Is there a reason why you have to store the shell script in a separate file? Typically, you would put it inline, within the AppleScript code. As far as I know, the “do shell script” command only operates on text, not on a script at a file path. If you give it a variable that contains a path, it will try to run that path as a command. It won’t run the contents of the file as a command.
Here is an example of an AppleScript that runs an inline shell script and puts the results in TextEdit:
property theShellScript : "#!/bin/bash
echo Hello World"
tell application "TextEdit"
activate
set theScriptResult to do shell script theShellScript
make new document
set the text of document 1 to theScriptResult
end tell
… you can of course replace the above shell script with the contents of your own shell script.
If you do need to keep the script in a separate file, the best way to do that is probably to save your AppleScript as an Application, and put the shell script within the Application bundle. “Path to me” is the path of the application that is running the script — not to the script itself — but if you save your AppleScript as an Application, then it runs its own script, and “path to me” works as you originally expected.
Here is an example of an AppleScript that runs a shell script contained within a file that is stored within its own application bundle:
property theApplicationPath : the path to me as text
property theShellScriptPath : theApplicationPath & "Contents:Resources:Scripts:bash.sh"
tell application "TextEdit"
open alias theShellScriptPath
set theShellScript to the text of document 1
set theScriptResult to do shell script theShellScript
make new document
set the text of document 1 to theScriptResult
end tell
With the above script Copy/Pasted into a new document in AppleScript Editor, hold down the Option key and choose File ▶ Save As, and in the Save dialog box, on the File Format pop up menu, choose “Application” and of course give your application a name and click Save. Then in Finder, navigate to where you Saved your application, and 2-finger tap (or right-click) on your application and choose “Show Package Contents.” That opens your application up as a folder, exposing the file system within. Put your shell script file named “bash.sh” inside the folder “Contents/Resources/Scripts” within your application and then close the window that represents your application.
Now when you run your application from anywhere in the file system, it will still be able to find and run its incorporated shell script.
Related
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.
Just 3 lines of script to be able to test a droplet application without leaving applescript editor
set fich to POSIX file "/Appli/conv2spct.app" as string
tell application "Finder" to open POSIX file "/Users/yourusername/Desktop/somefile" using application file fich
If there are errors in your droplet a display dialog will be opened by the script editor applescript
The same script with choose file for the 2 elements
set fileappli to POSIX path choose file of type {"APPL"} with prompt "Choose a Droplet application to debug"--the droplet for debug
set fileargument to POSIX path choose file --the file argument to pass at droplet
tell application "Finder" to open fileargument using application file fileappli
If there are errors in your droplet a display dialog will be opened by the script editor applescript
Here's a pragmatic alternative using do shell script, which potentially allows you to specify multiple file arguments:
do shell script "open -a /Appli/conv2spct.app ~/Desktop/somefile1 ~/Desktop/somefile2"
The above paths happen not to need quoting (escaping) for the shell, but when using variables to specify the file paths, it's best to use quoted form of (to pass multiple arguments, apply quoted form of to each):
do shell script "open -a " & quoted form of fileappli & " " & quoted form of fileargument
Looks like this has become simpler since the question was first asked. According to this documentation you can write:
open {choose file}
on open theDroppedItems
...
end open
Run this from within the AppleScript editor and the file you choose will be opened as if it had been dropped onto the compiled script.
I'm trying to make script or automate unrar to unrar a selected file to a specific folder (hard coded).
I want the following code to be run in terminal by clicking a button in finder or a keyboard shortcut while I have a file selected.
unrar e <path_to_selected_file.rar> <hard_coded_path>
How can I do this in the best way?
If your destination path is hardcoded, then I suggest you to use Automator.
First create a Service. Select on top, "get the file" in application "Finder".
Then add only one action : "run an Applescript".
In that action, the default script starts with variable "input". This variable will contains the list of all selected files while you're doing a right click on them in the Finder. Build your script to loop through files of that list, using POXIS function to convert the finder path (myUser:myfolder:myfile) to shell path (myUser/myfolder/myfile). With this path, use the "do shell script" command to run your "unbar" script.
When saved and tested, you can also define a shortcut key for that Service (in System Preferences).
Here is the script which should be in your Applescript Action :
on run {input, parameters}
set Destination to path to desktop folder -- User Desktop by default. can be changed
set PosixDest to POSIX path of Destination
set SelectedFiles to input
repeat with myFile in SelectedFiles -- loop through each selected file
set PosixF to POSIX path of myFile -- convert Finder path to Unix path
try -- try block to handle error during unbar
do shell script "unrar e " & (quoted form of PosixF) & " " & (quoted form of PosixDest)
end try
end repeat -- next file
return input
end run
This example is running as long as you select compressed file (to accept the unbar command). To be more safe, you should just add a test to your file, to check if it is a file OK for unbar. If not, just do nothing.
New to AppleScript but I'm trying to migrate over from terminal scripting. After much research I am having issues trying to get a shell script running from within the .app file.
What I have so far:
to the_foo()
tell application "Finder"
set current_path to container of (path to me) as alias
set path_in_posix to POSIX path of current_path
end tell
tell application "Terminal"
set new_terminal to do script " "
activate
do script "cd " & path_in_posix in window 1
do shell script "ls " & POSIX path of (path to me) & "Contents/Resources/Scripts/foobar.sh" in window 1
end tell
end the_foo
The error I am getting:
Learned to open a new terminal with: Applescript to open a NEW terminal window in current space
I added in window 1 when I learned that do script opens a new terminal window every time, referenced: applescript and terminal ( run several do shell script in one terminal window )
I originally tried:
set script_Location to path to resource "Contents:Resources:Scripts:"
set run_Script to (quoted form of script_Location) & "foobar.sh"
do shell script run_Script
after referencing: How to change AppleScript path to a Terminal-style path? but when I run it I get the same error.
So how can I run the shell script located within the Scripts folder within the same window 1? I would ideally like to set a variable for the path so I can put multiple shells scripts in the Scripts folder.
It's probably just a typo
do script "ls " & POSIX path of (path to me) & "Contents/Resources/Scripts/foobar.sh" in window 1
rather than
do shell script "ls " & ...
I recommend to use System Events instead of the Finder to get the container of the script
tell application "System Events"
set path_in_posix to POSIX path of container of (path to me)
end tell
Vadian had the correct and better approach in a one liner. I did change
"Contents/Resources/Scripts/foobar.sh"
to
set script_Location to "Contents/Resources/Scripts/"
set foobar to do script "bash " & POSIX path of (path to me) & script_Location & "foobar.sh" in window 1
this approach helps if I want to add more than one shell script in the Scripts folder.
I am packaging an application into a .app directory for "drag install" or whatever it's called and I have a weird iessue with file association.
I set my application as a viewer for .xyz files, and the system does start my app when I double click that file; the only problem is that the path of the file I clicked is nowhere in the args[], there's only one parameter that is something like ~psn_0_901340 and I think is a timestamp because it changes every time.
So... what am I supposed to do? I've been sitting here for 2 hours straight and can't find a solution.
I think what you want is an AppleScript droplet.
A shortened version of the AppleScript from that link:
on open dropped_files
set the_command to quoted form of POSIX path of (path to resource "script.sh")
set file_list to ""
repeat with file_path in dropped_files
set file_list to file_list & " " & quoted form of POSIX path of file_path
end repeat
set the_command to the_command & file_list
do shell script the_command
end open
Export as an application using Script Editor. Place script.sh in the Resources folder.
Add your file extension associations to Info.plist. You may need to launch or move the droplet before OS X notices the change & allows you to double-click files.
If you want to launch Terminal or capture the script output, see the full AppleScript.