Tuesday, September 21, 2021

#1 2021-09-12 07:18:03 am

kiwilegal
Member
From:: France
Registered: 2006-01-20
Posts: 350

Unable to work out Posix from HFS - duplicating files

Hi

Long time no post. But I have run out of ideas on a script, where I am sure someone will put me quickly right.


set Ainput_Folder to (((path to home folder as text) & ":Input (multiple files use ZIP):"))
set Aoutput_Folder to (((path to home folder as text) & ":Output:"))

set input_Folder to Ainput_Folder as alias
set output_Folder to Aoutput_Folder as alias

set Posix_input_Folder to quoted form of (POSIX path of (input_Folder))
set Posix_output_Folder to quoted form of (POSIX path of (output_Folder))



tell application "System Events" to set files_to_Move to every file of folder Ainput_Folder whose name extension is "PDF"
repeat with moveFile in files_to_Move
    tell application "Finder"
        duplicate moveFile to Aoutput_Folder with replacing
    end tell
end repeat


I have never properly understood when one must use POSIX, quoted Posix, HFS, colons etc, when dealing with folder paths and file paths. In my example, I have tried using  Aoutput_Folder, output_Folder and Posix_output_Folder, as the proper output folder for duplicating. None work, throwing up different errors.

I am trying to duplicate selected files into the output folder. The selection part works fine. I just cannot get the duplication to work.

Please can someone help. I have searched the forums and tried (as you can see), but it is just not working.

Cheers

Andrew

Offline

 

#2 2021-09-12 08:27:26 am

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 105

Re: Unable to work out Posix from HFS - duplicating files

The issues that interfered with your script was first, including an extra colon in your first two lines, and second, that you didn't specify that moveFile was an alias when you attempted to duplicate it. NB I'm using 10.12 so who knows what will happen if you're using the newest OS.

Applescript:

set Ainput_Folder to ((path to home folder as text) & "input:")
set Aoutput_Folder to ((path to home folder as text) & "output:")

set input_Folder to Ainput_Folder as alias
set output_Folder to Aoutput_Folder as alias

tell application "System Events"
   set files_to_Move to every file of folder Ainput_Folder whose name extension is "PDF"
   repeat with moveFile in files_to_Move
       tell application "Finder"
           duplicate (moveFile as alias) to output_Folder with replacing
           -- duplicate (moveFile as alias) to (Aoutput_Folder as alias) with replacing
       end tell
   end repeat
end tell

I included a variant on the duplicate line, which specifies that the output folder is an alias on the fly. If using that line, you could delete lines 3 and 4. I don't think there would be a performance hit either way but this way you could reduce your code by two lines. Finally, when posting, if you select your text and click the 'Applescript' button, it will format the code properly for the board.

Oh… as far as posix is concerned… when you are working with a shell script or app that requires a posix file specifier, then use it. Otherwise, in most cases you can stick with 'alias' (assuming that your file exists). If the file does not yet exist, then 'file' may be appropriate. You can see Apple's guidelines here in the Language Guide under 'Aliases and Files':

https://developer.apple.com/library/arc … CH218-SW28

Last edited by Mockman (2021-09-12 08:28:42 am)

Offline

 

#3 2021-09-12 08:39:55 am

kiwilegal
Member
From:: France
Registered: 2006-01-20
Posts: 350

Re: Unable to work out Posix from HFS - duplicating files

Thank you very much for this. Much appreciated.

I had also just found an old exchange I had with with StefanK back in 2011!!

https://macscripter.net/viewtopic.php?id=37702

He had a brilliant command line solution for this:

do shell script "mdfind -onlyin " & sourceFolder & " -0 'kMDItemFSName = \"*.mov\"' | xargs -0 -J {} cp {} " & destinationFolder.


I just changed the ".mov" to ".pdf" and bang! Quick too, of course.

I have Big Sur. Stefan's solution still works!

Really glad Macscripter has been around a long time:)

Best

Andrew

Offline

 

#4 2021-09-12 08:42:11 am

kiwilegal
Member
From:: France
Registered: 2006-01-20
Posts: 350

Re: Unable to work out Posix from HFS - duplicating files

Thank you very much for this. Much appreciated.

I had also just found an old exchange I had with with StefanK back in 2011!!

https://macscripter.net/viewtopic.php?id=37702

He had a brilliant command line solution for this:

Applescript:

do shell script "mdfind -onlyin " & sourceFolder & " -0 'kMDItemFSName = \"*.mov\"' | xargs -0 -J {} cp {} " & destinationFolder.

I just changed the ".mov" to ".pdf" and bang! Quick too, of course.

I have Big Sur. Stefan's solution still works!

Really glad Macscripter has been around a long time:)

Best

Andrew

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)