I am trying to copy a file whose path has spaces in it, and I can’t figure out how to do it.
-- Proof of concept, not the final code
repeat with theline in thelist
set z to the theline -- just for diagnostic purposes
-- example item 1 contents "23e2df087376f1cdb8b86ca1dd28084b /Users/name/Library/Mail/V8/19D44A4B-70C9-48D4-9CEA-0783B4E1C47A/Mailboxes.mbox/AAF old messages.mbox/35D025FF-59CF-4499-8B42-AC3255CD70BF/Data/1/8/8/Messages/881435.emlx"
-- remove the initial md5 hash
set atid to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set thefile to text items of theline
set thefile to items 2 thru (count of thefile) of thefile
set thefile to thefile as text
set AppleScript's text item delimiters to atid
-- thefile ends as "/Users/name/Library/Mail/V8/19D44A4B-70C9-48D4-9CEA-0783B4E1C47A/Mailboxes.mbox/AAF old messages.mbox/35D025FF-59CF-4499-8B42-AC3255CD70BF/Data/1/8/8/Messages/881435.emlx"
set sourceFile to thefile
set destFolder to "~/deleted mail/"
set destName to "myname.eml"
copy object sourceFile to folder destFolder new name destName with replacing and return path
end repeat
thelist has a series of lines with the first element being an md5 hash, followed by the file pathname, which happens to have spaces in the path.
If I set sourceFile to thefile as above, copy fails because the last element in the path (i.e. the actual filename) does not exist. If I use quoted form, then copy fails because the full path (with single quotes around) does not exist. I have copied the failing path the terminal, and it works there.
FileManagerLib v2.3.5
Mac OS X 11.6.7
Script Debugger v8.0.10
@ShaneStanley Thanks for the reply, and sorry if I was not clear.
If I don’t use the quoted form and just pass:
into the FileManagerLib copy, the script fails with “The file “881435.emlx” doesn’t exist.”, so it seems to have stripped off the rest of the path. The file is definately there, if I copy the full path from script debugger to terminal, ls finds it OK.
Hi @ShaneStanley . I have simplified the script and it seems to be a problem with the script when the destination folder doesn’t exist. (It’s not a permissions issue (ScriptDebugger has full disk access).)
use AppleScript version "2.4" -- Yosemite (10.10) or later
use script "FileManagerLib" version "2.3.5" # available at : <www.macosxautomation.com/applescript/apps/>
use scripting additions
set sourceFile to "/Users/name/Library/Mail/V8/19D44A4B-70C9-48D4-9CEA-0783B4E1C47A/Mailboxes.mbox/AAF old messages.mbox/35D025FF-59CF-4499-8B42-AC3255CD70BF/Data/1/8/8/Messages/881435.emlx" -- user name disguised
-- check that the file does actually exist
set x to do shell script "ls -l " & quoted form of sourceFile
display dialog x
set destFolder to "~/Desktop/Remove Duplicate Messages/Archived Duplicates"
set destName to "myname.eml"
-- this copy succeeded
copy object sourceFile to folder destFolder new name destName with replacing and return path
-- set destFolder to one that does not exist
set destFolder to "~/deleted mail"
-- this copy fails
copy object sourceFile to folder destFolder new name destName with replacing and return path
Copy functions correctly when the destination exists. The problem occurs when the destination folder for the FileManagerLib copy does not exist, but the error message thrown is incorrect (or there is a problem with the script):
@ShaneStanley thanks for your reply. If you can’t pass a folder that doesn’t exist, should the library script check for that, and other similar situations? From a quick scan it seems there are ‘subroutines’ that construct proper filename or folder references, so perhaps checking for existence (or non-existance as applicable) could be done in just a few places?
The suggestion for a check is particularly relevant if the error message thrown is misleading (as in this example).
It could – the trade-off is a little more overhead, plus extra code. Because of the way AppleScript works, you really don’t want bloat in libraries, or it will affect the size of scripts that can use them – it’s a balancing act.
There’s nothing to stop users adding exists object or create folder at calls where there’s any doubt.
To be honest, it didn’t occur to me in this situation, because you can’t move to a non-existent folder with any of the tools I’m used to.