Moving files to folder help

Hello all, I am trying to move a certain file from a mounted volume to a folder using Applescript but I keep getting different errors depending on the code I use. For the code snippet below I am getting “error “Finder got an error: AppleEvent handler failed.” number -10000”


tell application "Finder"
		move POSIX file "/Volumes/(Mounted Disk)/InstallFiles/(File)" to POSIX file "/Users/" & username & "/Desktop/(File)" with replacing
	end tell

Any help would be greatly appreciated.

Thanks,
CamSox

As far as I know, POSIX file belongs to the scripting addition Standard Additions so it must be used out of a tell application block.

Try with :

set theSource to POSIX file "/Volumes/(Mounted Disk)/InstallFiles/(File)"

set theDest to (path to desktop as text) & "destination" # Must be a path to a folder, not a file
tell application "Finder"
	move theSource to folder theDest
end tell

or with :

set theSource to POSIX file "/Volumes/(Mounted Disk)/InstallFiles/(File)"

set theDest to ((path to desktop as text) & "destination") as «class furl» # Must be a path to a folder, not a file
tell application "Finder"
	move theSource to theDest
end tell

The late one is my preferred one.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 25 mars 2019 10:37:13

Nope. This works fine:

tell application "Finder"
		move POSIX file "/Users/CK/Desktop/sample.txt" to POSIX file "/Users/CK/Desktop/Untitled Folder/" with replacing
end tell

provided the destination folder already exists.

Applying it to the OP’s original case, this code might be appropriate:

tell application "Finder"
		move POSIX file "/Volumes/[Mounted Disk]/InstallFiles/[File]" to the desktop with replacing
end tell

AppleScript: 2.7
Browser: Safari 605.1.15
Operating System: macOS 10.13

Although it appears in Standard Additions, I suspect that’s really just a way to document its existence. The fact that it works even in the absence of a “use scripting additions” statement would tend to confirm this. It’s just another way of creating a «class furl» file object.

Edit: This assumes there’s at least one other use statement, to make “use standard additions” compulsory for addition commands to compile.

Sorry, but you are wrong here. The interpreter searches for the command handler, first among the commands of Applescript itself. Not finding such, he automatically searches for the “owner” of the command in Standard Additions. If he does not find it there, he searches among any third-party additions installed on the computer. That is, the absence of statement “Use Standard Additions” does not guarantee that the command is not processed by them.
If the command is located in the tell block of the application, the search begins with the handlers of the application itself.

I should have mentioned that my test needs to be done in the presence of at least one other use statement. So consider this code:

use AppleScript version "2.4" -- Yosemite (10.10) or later
--use scripting additions

path to me
set x to POSIX file "/Applications"

It won’t compile unless you uncomment the use scripting additions command, because path to belongs to Standard Additions. But the line containing POSIX file will compile, with or without the use scripting additions.

I’ll change the post to make it clearer.

Yes, in this case, you must specify “Use Scripting Additions”. But only because you explicitly use only Applescript 2.4. Comment out the first line too and you will see that everything compiles perfectly.

Well, yes. But that also defeats the point of the exercise, which is to establish if POSIX file belongs to Standard Additions, or is actually defined elsewhere.

Well, now I see. But it must be understood by others, therefore I will leave our dialogue as is

Hello all of you.

II’m really puzzled because here the script behaves as I wrote.


# Variables defined for the original code
set userName to short user name of (system info)
set posixSourceFolder to "/Volumes/Macintosh HD/Macintosh …2.0/Dossier ClarisWorks 5.0/"
set fileName to "Ouvrez-moi ClarisWorks 5.0 copie"

# Variables used by added instructions
set SourceFile to POSIX file (posixSourceFolder & fileName) # out of tell application block
set destFolder to POSIX file ("/Users/" & userName & "/Desktop/") # out of tell application block
set destFile to POSIX file ("/Users/" & userName & "/Desktop/" & fileName) # out of tell application block

tell application "Finder"
	try
		move POSIX file (posixSourceFolder & fileName) to POSIX file ("/Users/" & userName & "/Desktop/" & fileName) with replacing
	on error errmsg number errNbr
		display dialog errmsg & linefeed & "error number " & errNbr buttons {"OK"}
		(*
display dialog "Erreur dans Finder : Il est impossible d’obtenir POSIX file \"/Volumes/Macintosh HD/Macintosh …2.0/Dossier ClarisWorks 5.0/Ouvrez-moi ClarisWorks 5.0 copie\".
error number -1728"
*)
	end try
	try
		move POSIX file (posixSourceFolder & fileName) to POSIX file ("/Users/" & userName & "/Desktop/") with replacing
	on error errmsg number errNbr
		display dialog errmsg & linefeed & "error number " & errNbr buttons {"OK"}
		(*
display dialog "Erreur dans Finder : Il est impossible d’obtenir POSIX file \"/Volumes/Macintosh HD/Macintosh …2.0/Dossier ClarisWorks 5.0/Ouvrez-moi ClarisWorks 5.0 copie\".
error number -1728"
*)
	end try
	
	try
		move SourceFile to destFile with replacing
	on error errmsg number errNbr
		display dialog errmsg & linefeed & "error number " & errNbr buttons {"OK"}
		(*
display dialog "Erreur dans Finder : Impossible de convertir file \"SSD 500:Users:**********:Desktop:Ouvrez-moi ClarisWorks 5.0 copie\" en type folder.
error number -1700"
*)
	end try
	move SourceFile to destFolder with replacing # Behaves flawlessly
end tell

As you may see, I must use POSIX file out of the tell application block.

Is it due to the fact that I use the system in french or is this behavior changed by Mojave ?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 28 mars 2019 12:16:38

I don’t believe it’s either. It seems that using POSIX file as an object specifier within the Finder tell block is acceptable only if the path is supplied in an explicit string form and its entirety without using any operations (concatenations). Variable references aren’t permitted either.

Compare:

tell application "Finder" to get POSIX file "/Users/CK/Desktop/sample.txt"

with:

tell application "Finder" to get POSIX file ("/Users/CK/Desktop/" & "sample.txt") --> Error -1728

However, if you use POSIX file as a class type, you can coerce values stored in variables and combined with other values as one would expect:

tell application "Finder" to get ("/Users/CK/Desktop/" & "sample.txt") as POSIX file

I don’t know why this is.

Even then, it’s of no real use. Try this:

tell application "Finder" to name of POSIX file "/Users/CK/Desktop/sample.txt"

I suspect the real issue Yvan is seeing is simply that Finder commands don’t like «class furl» file objects. It wants aliases or its own file types.

But I’m struggling to explain why he has a line he says is behaving flawlessly.

Hi Yvan.

I’m finding that ‘POSIX file’ + text variable errors in a Finder ‘tell’ statement, whereas ‘POSIX file’ + actual text doesn’t. (I see CK has just posted a similar observation.) In the latter case, the specifier is probably resolved by the compiler, whereas with a variable, the resolution happens at run time and is left to the Finder, which doesn’t understand the ‘POSIX file’ keyword. Apparently though, it understands the objects produced (for moving purposes, at least).

More and more puzzling.

I rarely speak to Finder so I don’t remember having problem when asking Finder to work upon «class furl» objects.
But as I am curious I made a new test.

set posixSourceFolder to "/Volumes/Macintosh HD/Macintosh …2.0/Dossier ClarisWorks 5.0/"
set fileName to "Ouvrez-moi ClarisWorks 5.0 copie"

set sourcePath to posixSourceFolder & fileName
--> /Volumes/Macintosh HD/Macintosh …2.0/Dossier ClarisWorks 5.0/Ouvrez-moi ClarisWorks 5.0 copie
set sourceFile to POSIX file sourcePath
--> file Macintosh HD:Macintosh …2.0:Dossier ClarisWorks 5.0:Ouvrez-moi ClarisWorks 5.0 copie
log (get class of sourceFile) --> «class furl»
set destFolder to POSIX file (POSIX path of (path to desktop as text))
--> file SSD 500:Users:**********:Desktop:
log (get class of destFolder) --> «class furl»
tell application "Finder"

	duplicate sourceFile to destFolder with replacing
	--> document file "Ouvrez-moi ClarisWorks 5.0 copie" of folder "Desktop" of folder "**********" of folder "Users" of startup disk
	move sourceFile to destFolder with replacing
	--> document file "Ouvrez-moi ClarisWorks 5.0 copie" of folder "Desktop" of folder "**********" of folder "Users" of startup disk of application "Finder"
	try
		name of sourceFile
	on error errMsg number errNbr
		display dialog errMsg & linefeed & "error number " & errNbr
		(*
display dialog "Erreur dans Finder : Il est impossible d’obtenir name of file \"Macintosh HD:Macintosh …2.0:Dossier ClarisWorks 5.0:Ouvrez-moi ClarisWorks 5.0 copie\".
error number -1728"
*)
	end try
end tell

Once again, like the duplicate one, the move instruction applied to «class furl» objects behaved flawlessly but the instruction asking for the name of the «class furl» item failed.

I guess that this will push me to stay far from Finder every time I don’t need to apply to its icons or its windows.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 28 mars 2019 14:35:58

Sure—one needs to include an additional file or item specifier before POSIX file. But, even so, I wouldn’t say it’s of absolutely no use, as this operation works:

tell application "Finder"
		move POSIX file "/Users/CK/Desktop/sample.txt" to the folder named "Untitled Folder"
end tell

(“Untitled Folder” being a pre-existing folder on the desktop). This is all illustrative, of course: I try to avoid getting Finder to do anything if I can help it, and for file operations using vanilla AppleScript, System Events is most often my preference.