3

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?

1
  • Does saving the script as an application make a difference? Commented Jul 26, 2014 at 11:46

4 Answers 4

2

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"
Sign up to request clarification or add additional context in comments.

2 Comments

other than it being one line how is this any different from what I posted ?
This is run without having to go through the Terminal app of activating, etc. And for the purpose of clean scripting, I condensed your 7 line, 260 characters into one line with 94 characters.
1

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

3 Comments

Well on my mac the 'path to me' doesn't always equal the path where applescript is located in. Unfortunately I need it to run bash.sh relative to the applescript path
have you tried taking path to me out of the terminal tell block ?
It doesn't make any difference, its still referencing the Apple CoreServices system folder
1

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.

1 Comment

Simon, the second part or your answer is probably what the OP is in need of but that's just a guess. As far as storing the bash script in a separate file I would guess that it is lengthy and it could be very difficult to mix the two languages. That's my two cents ;)
0

What does it mean that the user is in some Finder folder? This means that it is in the front Finder window that displays some folder or desktop.

That is, you need to determine the Posix path of the target folder of the front Finder window and pass it to the Terminal application.

tell application "Finder"
    if (count windows) = 0 then
        set aTarg to desktop as alias
    else
        tell front window to set aTarg to target as alias
    end if
end tell

tell application "Terminal" to if (count (windows whose visible is true)) = 0 then
    do script "cd " & quoted form of (POSIX path of aTarg)
else
    do script "cd " & quoted form of (POSIX path of aTarg) in front window
end if

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.