0

I have a following AppleScript:

tell application "Finder"
    set current_path to container of (path to me) as string
    duplicate file (current_path & "foo.txt")
end tell

It works, but I need to pass it an osascript expression. That is, the whole workflow looks like:

  1. Open the Terminal app

  2. Execute

    cd ~/test
    

    (the folder in which the foo.txt file and the bar folder are located).

  3. Execute

    osascript -l AppleScript -e 'tell application "Finder"' -e 'set current_path to container of (path to me) as string' -e 'duplicate file (current_path & "foo.txt") --to folder (current_path & "bar")' -e 'end tell'
    

The problem is that current_path is now treated to be Macintosh HD:usr:bin and not Macintosh HD:Users:user:john:test. How to fix this?

2 Answers 2

2

One option is to pass the directory into osascript as a parameter:

#!/usr/bin/env zsh

osascript -l AppleScript - ~/test << EOF
  on run argv
    set work_path to ((POSIX file (item 1 of argv)) as alias) as string
    tell application "Finder"
      duplicate file (work_path & "foo.txt") to (work_path & "bar")
    end tell
  end run
EOF
  • on run argv ... - per the osascript man page, this is how you access the command line parameters passed from the shell.
  • (item 1 of argv) - the first parameter.
  • (POSIX file (...) - converts a string in the form /a/b/c to a path.
  • (...) as alias - converts the POSIX path into a vol:dir:dir:... object.
  • (...) as string - converts the alias into a vol:dir:dir:... string so that append will work.
  • tell... - the file duplication command.

<< EOF ... EOF is a here document. The lines that follow are passed to the stdin of osascript. It looks like it might be a bit more convenient than a set of -e parameters.

2
  • Many thanks, Gairfowl. This also works with copy: copy file (work_path & "foo.txt") to folder (work_path & "bar"). But if I change work_path & "bar" to work_path & "../bar" to copy the file to the bar directory one level above, this doesn't work. Do you know how this could be fixed? (The whole version is here: pastebin.com/raw/2bSaPE3c)
    – jsx97
    Commented Mar 18 at 9:58
  • 1
    This line, inside the tell block, appears to get the parent folder: set parent_path to (parent of (work_path as alias)) as string (its from this answer). You can then change work_path & "bar" to parent_path & "bar".
    – Gairfowl
    Commented Mar 18 at 11:41
1

Another option would be to use regular shell expansions in the heredoc, for example:

osascript << SCRIPT
set current to "$(pwd)" as POSIX file as text
tell application "Finder" to duplicate file (current & "foo.txt") to folder (current & "bar")
SCRIPT

Also, if using osascript to run a regular script file, NSProcessInfo can be used to get the environment the process was launched from. For example, if the following script was saved somewhere:

use framework "Foundation"
use scripting additions

set processArgs to (current application's NSProcessInfo's processInfo's environment) as record
set current to (PWD of processArgs) as POSIX file as text
tell application "Finder" to duplicate file (current & "foo.txt") to folder (current & "bar")

Then from the terminal you could cd to whatever directory and use osascript /path/to/the.scpt.

2
  • Many thanks, red_menace. Regarding the first verion, could you explain why changing folder (current & "bar") to folder (current & "../baz") doesn't do what I expect? That is, it doesn't copy the file to the folder baz located in the parent folder.
    – jsx97
    Commented Mar 18 at 16:57
  • 1
    @jsx97 - The double dot is more of a command shortcut than a text substitution, but you can get the parent folder by using commands, for example set destination to "$(dirname $PWD)/baz" as POSIX file as text, or have the Finder get the container, for example set destination to (container of folder current) as text & "baz".
    – red_menace
    Commented Mar 18 at 21:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .