Get current_path if using AppleScript as osascript expression

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?

1 Like

The following worked on my Sonoma computer. It’s intended to be run in a terminal, but I’m posting it as a script just to maintain quoting:

osascript -l AppleScript -e 'set current_path to do shell script "pwd"' -e 'set current_path to ((POSIX file current_path) as text)' -e 'tell application "Finder"' -e 'duplicate file (current_path & "foo.txt")' -e 'end tell'
1 Like

Thank you so much, peavine.

Here I have changed your version so that it duplicate a file to another folder and not the same:

~/
 |- test/
    |- foo.txt
    |- bar/
osascript -l AppleScript -e 'set current_path to do shell script "pwd"' -e 'set current_path to ((POSIX file current_path) as text)' -e 'tell application "Finder"' -e 'duplicate file (current_path & "foo.txt") to folder (current_path & "bar")' -e 'end tell'

This successfully dupicates foo.txt to bar folder, but how to make it to duplicate a file, using a relative path, to a parent folder (test), or maybe to another folder in the parent folder (test2)?

~/
 |- test/
 |  |- foo.txt
 |  |- bar/
 |
 |- test2/

I have tried to change current_path & "bar" to current_path & "../test2" and current_path & "::test2", but nether version works. The script doesn’t throw an error, it simply draws an empty line.

osascript -l AppleScript -e 'set current_path to do shell script "pwd"' -e 'set current_path to ((POSIX file current_path) as text)' -e 'tell application "Finder"' -e 'duplicate file (current_path & "foo.txt") to folder (current_path & "../test2")' -e 'end tell'

The following duplicates foo.txt in folder test2:

osascript -l AppleScript -e 'set current_path to do shell script "pwd"' -e 'set current_path to ((POSIX file current_path) as text)' -e 'tell application "Finder"' -e 'set target_path to ((container of folder current_path) as text)' -e 'duplicate file (current_path & "foo.txt") to folder (target_path & "Test2:")' -e 'end tell'

The following duplicates foo.txt in the parent folder of the containing folder of foo.txt.

osascript -l AppleScript -e 'set current_path to do shell script "pwd"' -e 'set current_path to ((POSIX file current_path) as text)' -e 'tell application "Finder"' -e 'set target_path to ((container of folder current_path) as text)' -e 'duplicate file (current_path & "foo.txt") to folder target_path' -e 'end tell'

I suspect you could also get the parent folder with the shell, but the approach used above was easier for me.

1 Like

A heredoc can also be used with osascript, which can be pasted in the Terminal the same way. I find long(er) scripts are a bit easier to read, for example:

osascript << SCRIPT
set currentFolder to "$(pwd)" as POSIX file as text -- shell command
# set parentFolder to "$(dirname $PWD)" as POSIX file as text -- shell command using built-in variable
tell application "Finder"
   set parentFolder to (container of folder currentFolder) as text
   duplicate file (currentFolder & "foo.txt") to folder (currentFolder & "bar")
   # duplicate file (currentFolder & "foo.txt") to folder (parentFolder & "test2")
end tell
SCRIPT

I also included getting the parent folder with the shell.

2 Likes

On Sierra… I get a paucity of colons error. This works for me though:

osascript << SCRIPT
set currentFolder to "$(pwd)" as POSIX file as text -- shell command
tell application "Finder"
   set parentFolder to (container of folder currentFolder) as text
   duplicate file (currentFolder & ":foo.txt") to folder (currentFolder & ":bar:")
end tell
SCRIPT

The cause being that the result of pwd doesn’t include a trailing slash, and this is reflected in missing colons after the currentFolder and ‘bar’, resulting in an attempt to work with imaginary objects. The error in the terminal was:

296:372: execution error: Finder got an error: 
Can’t set folder "MacHD:Users:username:Desktop:testbar" to 
file "MacHD:Users:username:Desktop:testfoo.txt". (-10006)  

Thanks also for providing a good heredoc example.

Updated to specify that issue occurs on Sierra
Details in subsequent posts

That is odd, what OS are you running? I am running macOS Sonoma, and when retesting my original post works OK, but your fix has errors from extra colons. While the pwd or dirname commands do not include the trailing path delimiter, at least with Sonoma it would seem that the coercion to POSIX file is adding one.

I’m running Sierra so that is likely the issue.

That coercion is different on Sierra.

osascript -e "set currentFolder to \"$(pwd)\" as POSIX file"        
file MacHD:Users:username:Desktop:test

osascript -e "set currentFolder to \"$(pwd)\" as POSIX file as text"
MacHD:Users:username:Desktop:test

When I run my version, I get the following:

% pwd
/Users/username/Desktop/test

% osascript << SCRIPT
set currentFolder to "$(pwd)" as POSIX file as text -- shell command
tell application "Finder"
   set parentFolder to (container of folder currentFolder) as text
   duplicate file (currentFolder & ":foo.txt") to folder (currentFolder & ":bar:")
end tell
SCRIPT

document file foo.txt of folder bar of folder test of folder Desktop of folder username of folder Users of startup disk

Many thanks and sorry for the delay in responding.

So I have two optios to get the parent folder: with the shell or with AppleScript.

Here they are, for convenience:

(1) With the shell:

osascript << SCRIPT
set currentFolder to "$(pwd)" as POSIX file as text 
set parentFolder to "$(dirname $PWD)" as POSIX file as text
tell application "Finder"
   duplicate file (currentFolder & "foo.txt") to folder (parentFolder & "test2")
end tell
SCRIPT

(2) With AppleScript:

osascript << SCRIPT
set currentFolder to "$(pwd)" as POSIX file as text 
tell application "Finder"
   set parentFolder to (container of folder currentFolder) as text
   duplicate file (currentFolder & "foo.txt") to folder (parentFolder & "test2")
end tell
SCRIPT

My observation is that in (1) I can move set parentFolder inside tell application "Finder"; but in (2) I cannot move set parentFolder outside there. Could you explain why this difference exists?

Terminology used in an application tell statement targets the declared application.

In the first example, the shell is substituting the result of the $(dirname $PWD)command in the heredoc, and the coercions are standard and will work anywhere in a script. For the second example, a variable is being set using the scripting term container, which the Finder has declared to be a property of a file. Outside of the Finder tell statement though, container has no special meaning, which in this context would fail because that identifier isn’t a known property or variable.

1 Like