Home > Software design >  How to use relative path to a local file in Automator (macOS)
How to use relative path to a local file in Automator (macOS)

Time:10-09

I am having a problem defining a relative path to an HTML file from within an Automator workflow.

Problem:

The file structure is as follows:

portable folder

  • Automator.app
  • Chromium.app
  • assets folder -- HTML file (my-file.html)

I have a built a simple app using Automator which launches a new instance of Chromium (portable) and displays a local HTML file when executed. The path to the HTML file is the part I am having problems with.

The first part of the Automator workflow is an Applescript that sets the working directory to the location of the .app file.

on run {input, parameters}
    set p to path to me
    return p
end run

The second part of the Automator workflow is a shell script that is supposed to open the HTML file in the provided Chromium browser.

APP_PATH=$1
cd "$APP_PATH"
# open chromium from current directory
open -n -a Chromium.app --args --user-data-dir='/tmp/chrome_dev_test' --allow-file-access-from-files --allow-file-access --allow-running-insecure-content '/assets/my-file.html'

The browser opens as expected when my Automator app is double clicked and it attempts to open the HTML file but it can not find it. The address bar displays the path to the HTML file I have instructed it to open, so it is trying. The HTML file displays fine when dragged and dropped into the browser (no problems with the included --args).

I am not sure what to prepend to my file location in order for it to open the HTML file properly. With the cd "$APP_PATH" instruction, I believe I have set the working directory to be "portable folder" so the "/assets/my-file.html" would be the relative path to the file I want to open.

The Automator app and the assets folder that holds the HTML page will always be in the same folder so their relationship will always be the same. The entire folder containing all necessary files is meant to be portable so it does not have a defined location on a users system. It can be run off a USB key or the Desktop or wherever.

How do I format the URL in my shell script to recognize the full path to the HTML file given that it is portable but will always have the same location with respect to the "$APP_PATH" ?

CodePudding user response:

First of all p is an AppleScript alias specifier, you need a POSIX oath.
Second of all p points to the app itself but you need the reference to the parent folder.

System Events gives you the POSIX path of the parent folder. Append the folder name and the name of the HTML file

on run {input, parameters}
    set p to path to me
    tell application "System Events" to set parentFolder to POSIX path of container of p
    return parentFolder & "/assets folder/my-file.html"
end run

assets folder represents the name of the folder

  • Related