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'
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.
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.
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.