I’m upgrading applescripts that worked in Snow Leopard but fail in Yosemite.
I’ve tracked down one problem to the Finder’s “duplicate” command. It no longer appears to return a reference to the duplicated object.
set testPath to "MyVolume:Path:To:File:testfile.txt"
tell application "Finder"
set fileRef to duplicate (testPath as alias)
log fileRef as string
end tell
On a Yosemite Mac, the duplicate command works but the returned fileRef is undefined. On the log line, I get the error: “The variable fileRef is not defined.”
On a Snow Leopard Mac, the duplicate command works and the returned fileRef variable is fine. On the log line, I get: “MyVolume:Path:To:File:testfile copy.txt”
The Finder dictionary on the Yosemite machine says:
The duplicate command is supposed to return the specifier.
Anyone else run into this problem? Am I missing something? Is there a workaround?
For me, on Yosemite, the result is a variation of “The variable result is not defined.”
Interesting that there’s not more outcry. Seems like the Finder’s “duplicate” command is going to show up in a lot of scripts – it’s pretty low level. And having a reference to the duplicated file is essential for a lot of tasks. But I didn’t pull up much when searching for folks having troubles with it.
set dp to path to desktop
set f to choose file
tell application "Finder"
duplicate f to dp
end tell
(* result:
document file "TestFile.rtf" of folder "Desktop" of folder "kelhome" of folder "Users" of startup disk of application "Finder"
*)
It works.
Edited: this also works when run as application:
set dp to path to desktop
set f to choose file
tell application "Finder"
set newf to (duplicate f to dp) as alias as string
end tell
display dialog newf
It might have to do with the fact that your problem is hard to reproduce. I ran:
set testPath to "Macintosh HD:Users:shane:Desktop:Screen Shot.png"
tell application "Finder"
set fileRef to duplicate (testPath as alias)
log fileRef as string
end tell
And the log in Script Editor shows:
tell application "Finder"
duplicate alias "Macintosh HD:Users:shane:Desktop:Screen Shot.png"
--> document file "Screen Shot copy.png" of folder "Desktop" of folder "shane" of folder "Users" of startup disk
--> error number 0
get document file "Screen Shot copy.png" of folder "Desktop" of folder "shane" of folder "Users" of startup disk
--> "Macintosh HD:Users:shane:Desktop:Screen Shot copy.png"
(*Macintosh HD:Users:shane:Desktop:Screen Shot copy.png*)
end tell
That error looks odd, but the reference is being returned to the duplicate command.
You’re right, Shane. And Kel1. Your version does work when the file is local, just as you showed. I didn’t realize that my issue had to do with the file being located on a network share, being accessed over SMB (SMB2 I think?). And I’m relieved that it’s not a wider problem.
So I have a file “test.txt” located at the root of Oasis, the drive that’s on the network. test.txt contains the string: “This is some text.”
Here’s the Applescript, including a file read to demonstrate that the file is readable at the testPath variable:
set testPath to "Oasis:test.txt"
tell application "Finder"
set fileContents to (read file testPath as «class utf8»)
log fileContents
set fileRef to duplicate (testPath as alias)
log fileRef as string
end tell
Here’s the log result in Script Editor.
tell application "Finder"
read file "Oasis:test.txt" as «class utf8»
--> error number -10004
end tell
tell current application
read file "Oasis:test.txt" as «class utf8»
--> "This is some text.
"
end tell
tell application "Finder"
(*This is some text.
*)
duplicate alias "Oasis:test.txt"
--> current application
--> error number 0
Result:
error "The variable fileRef is not defined." number -2753 from "fileRef"
So yes, I get the same result with a file on a remote volume. (The error number -10004
is to be expected because you’re using a scripting addition command inside an application tell block.)
Yet another reason to avoid using the Finder – as if we needed one.
Reading the thread from 2010 that you linked to – and I wonder why I didn’t find it when I was searching – it seems that you were confronting the same issue. In your post #15 on that thread, you came up with a workaround. My dutch is a bit rusty, but I’m guessing " kopie" means " copy". So you’re just hard-coding the expected copy filename and going from there?
What need to complicate the life ?
I dislike the Finder but your goal may be easily reached.
set testPath to (path to desktop as text) & "HP OfficeJet Pro 8610e.rtf"
tell application "Finder"
set fileRef to get (duplicate (testPath as alias))
end tell
fileRef as text
Yvan KOENIG (VALLAURIS, France) samedi 8 novembre 2014 22:43:29
set testPath to "Oasis:test.txt"
# Don't call read file wich is a component of the OSAX Standard Additions in a tell application block !
set fileContents to (read file testPath as «class utf8»)
log fileContents
tell application "Finder"
set fileRef to get (duplicate (testPath as alias))
log fileRef as string
end tell
Yvan KOENIG (VALLAURIS, France) samedi 8 novembre 2014 22:48:08
I was hoping your script with the “set fileRef to get (duplicate …)” approach would work but, alas, it does not.
It probably works with your local file on the desktop. But it fails on the mounted network share.
Good point on moving the read file statement out of the tell block. That statement, though, wasn’t really a part of the problem. I just added it to the script to demonstrate that the testPath existed and was readable by the Finder. The key is the “set fileRef to duplicate…” which no longer works on the network share.