Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applescript run bash script from same directory

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?

like image 529
Ryan Avatar asked Nov 28 '25 09:11

Ryan


2 Answers

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"
like image 87
double_j Avatar answered Nov 29 '25 22:11

double_j


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
like image 44
mcgrailm Avatar answered Nov 30 '25 00:11

mcgrailm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!