0
votes

I'm trying to create a mac "app" using automator that basically calls a .command file to do all the work. The command file will be in the same dir as the .app but i'm falling at the first which is - get the current directory of the .app file thats been clicked to determine the file location of the .command file.

i've tried

SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
echo "-- $SCRIPTPATH"

This just returns my users director - basically ~ The app itself is in a dir on the Desktop example: ~/Desktop/foo/my.app

I've also tried

 here="`dirname \"$0\"`"
   echo "cd-ing to $here"
   cd "$here" || exit 1

neither work.

ultimately i need to call my.command to run the command but need to know its actual position - or even relative to the app so that it'll fire. currently i get the error that it can't find the my.command as its not located in the root of my user account (since i wont have control over where it can be placed on the end users machine).

Any pointers on what i can do to solve this much appreciated.

Note: To answer - why am i using an app which has a terminal script to call a .command which is essentially a script - basically because if you do it this way a terminal doesn't actually pop up.. which for this demo is what i need to happen.

1
Why not just use an Run Shell Script action and place the code that's in the .command file there?user3439894
You can use a Run AppleScript action to get the NSBundle's bundlePath, but as mentioned you can also use a Run Shell Script action.red_menace
@user3439894 @red_menace thanks - i noted why i was doing it this way but lets for arguments sake just perform it as ask - when i place the following code in the shell script for automator.. ``` here="dirname \"$0\"" /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --app=file://"$here"/mywebpage.htm ``` the same challenge exists.. $here evaluates to . meaning chrome fails with the error.. "dont' know how to open file://./mywebpage.htmglennE

1 Answers

0
votes

As you did not include explicit details of your Automator workflow, saved as an application, I'm presenting the following as an example of how to have and Automator app, e.g. my.app, execute the e.g. my.command script file, that which is located in the same folder as e.g. my.app is.

For the purpose of the example, I created a folder named foo on my Desktop, in which my.app was saved along with the my.command script file.

The Automator application workflow uses a Run AppleScript action to accomplish the goal.

Replace the default code with the following example AppleScript code:

set myCommandFilename to "my.command"

set myAppPathAlias to path to me

tell application "System Events"
    set myDirName to POSIX path of container of myAppPathAlias
    set myCommandFilePathname to myDirName & "/" & myCommandFilename
    set myCommandFilenameExists to exists file myCommandFilePathname
end tell

if myCommandFilenameExists then
    try
        do shell script myCommandFilePathname's quoted form
    on error eStr number eNum
        display dialog eStr & " number " & eNum ¬
            buttons {"OK"} default button 1 ¬
            with title "File I/O Error..." with icon stop
    end try
else
    display dialog "A necessary file, ' " & myCommandFilePathname & ¬
        "', is missing!" buttons {"OK"} default button 1 ¬
        with title "Missing File..." with icon stop
end if
  • Note: Change my.command to the actual filename. The rest of the example AppleScript code should not need to be modified.

Automator App

If my.app is launched and the my.command script file is not in the same folder as my.app, then an error message will be displayed, e.g.:

Missing File Error

If my.app is launched and the my.command script file doesn't have its executable bit set, then this error message will be displayed, e.g.:

File  I/O Error

Also, if the my.command script file does not exit cleanly, it too will display an error message, e.g.:

Dirty Exit

  • The content of the error message will vary based on the content of the e.g. my.command script file, how it's coded and how it fails. This example is worst case scenario in that it lets you know something failed, but not what failed.

Note: The example AppleScript code is just that and does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.