Saturday, August 17, 2019

#1 2019-03-24 07:15:09 pm

CamSox
Member
Registered: 2016-12-22
Posts: 5

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"

Applescript:


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

Offline

 

#2 2019-03-25 03:37:23 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3537

Re: Moving files to folder help

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 :

Applescript:

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 :

Applescript:

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

Last edited by Yvan Koenig (2019-03-25 04:42:53 am)

Offline

 

#3 2019-03-27 07:13:53 pm

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 96

Re: Moving files to folder help

Yvan Koenig wrote:

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.

Nope.  This works fine:

Applescript:

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:

Applescript:

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

Offline

 

#4 2019-03-27 08:54:42 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5818

Re: Moving files to folder help

Yvan Koenig wrote:

As far as I know, POSIX file belongs to the scripting addition Standard Additions



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.

Last edited by Shane Stanley (2019-03-27 10:55:33 pm)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2019-03-27 10:44:45 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 419

Re: Moving files to folder help

Shane Stanley wrote:
Yvan Koenig wrote:

As far as I know, POSIX file belongs to the scripting addition Standard Additions



The fact that it works even in the absence of a "use scripting additions" statement would tend to confirm this.



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.

Last edited by KniazidisR (2019-03-27 10:55:37 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#6 2019-03-27 10:53:35 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5818

Re: Moving files to folder help

KniazidisR wrote:

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 add-ons installed on the computer. That is, the absence of statement "Use Standard Additions" does not guarantee that the command is not processed by them.



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:

Applescript:

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#7 2019-03-27 11:10:41 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 419

Re: Moving files to folder help

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.

Last edited by KniazidisR (2019-03-27 11:11:40 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#8 2019-03-27 11:14:03 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5818

Re: Moving files to folder help

KniazidisR wrote:

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#9 2019-03-27 11:19:56 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 419

Re: Moving files to folder help

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

Last edited by KniazidisR (2019-03-27 11:21:50 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#10 2019-03-28 05:18:13 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3537

Re: Moving files to folder help

Hello all of you.

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

Applescript:


# 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

Offline

 

#11 2019-03-28 05:40:08 am

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 96

Re: Moving files to folder help

Yvan Koenig wrote:

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 ?

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:

Applescript:

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

with:

Applescript:

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:

Applescript:

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

I don't know why this is.

Offline

 

#12 2019-03-28 05:57:47 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5818

Re: Moving files to folder help

CK wrote:

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.



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

Applescript:

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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#13 2019-03-28 05:58:20 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4940

Re: Moving files to folder help

Yvan Koenig wrote:

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


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

Last edited by Nigel Garvey (2019-03-28 06:01:54 am)


NG

Offline

 

#14 2019-03-28 07:36:39 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3537

Re: Moving files to folder help

Shane Stanley wrote:


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.



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.

Applescript:

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

Last edited by Yvan Koenig (2019-03-28 08:09:08 am)

Offline

 

#15 2019-03-28 08:01:18 am

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 96

Re: Moving files to folder help

Shane Stanley wrote:
CK wrote:

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.



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

Applescript:

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

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:

Applescript:

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.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)