I’m trying to export attachments from Mail to a specific folder. If the folder doesn’t exist in the parent folder then the folder is created. If the folder doesn’t exist I need a reference to the folder. But I get an error message -1728. Here is the script
set attachmentsPath to "/Users/beatrixwillius 1/Documents/Development/Mail Archiver issues from users"
set attachmentsFolder to attachmentsPath as POSIX file
set Foldername to "servais <tickets@mothsoftware.uservoice.com>"
tell application "Finder"
try
set newFolder to (make new folder at attachmentsFolder with properties {name:Foldername})
on error errMsg number errNr
if errNr = -48 then
--folder already exists
set newFolder to get (every folder in attachmentsFolder whose name contains Foldername)
else
display dialog "Error: " & errMsg & " Nr.: " & errNr
return
end if
end try
end tell
I think the problem is that I get the attachmentsFolder as POSIX file. But what do I need to use instead?
I use relative HFS paths for similar tasks, because the Finder doesn’t work with POSIX paths. Also, I would write the try block other way:
set attachmentsHFSPath to "" & (path to documents folder) & "Development:Mail Archiver issues from users:"
set Foldername to "servais <tickets@mothsoftware.uservoice.com>"
tell application "Finder"
try
-- folder exists
set newFolder to folder Foldername of folder attachmentsHFSPath
on error
-- folder doesn't exist
try
set newFolder to (make new folder at folder attachmentsHFSPath with properties {name:Foldername})
on error errMsg number errNr -- some additional error
display dialog "Error: " & errMsg & " Nr.: " & errNr
end try
end try
end tell
I need to keep the Posix path because the script is going to be part of an Alfred workflow. The environment variables are Posix path. But I got it now:
set attachmentsPath to "/Users/beatrixwillius 1/Documents/Development/Mail Archiver issues from users"
set attachmentsFolder to attachmentsPath as POSIX file
set attachmentsAlias to attachmentsFolder as alias
tell application "Finder"
set newFolder to get (every folder in attachmentsAlias whose name contains "servais <tickets@mothsoftware.uservoice.com>")
end tell
It’s much easier to get the POSIX path from an HFS path than the other way round
set attachmentsFolder to "" & (path to documents folder) & "Development:Mail Archiver issues from users:"
set attachmentsPath to POSIX path of attachmentsFolder
tell application "Finder"
set newFolder to get (every folder in folder attachmentsFolder whose name contains "servais <tickets@mothsoftware.uservoice.com>")
end tell
The OP clarified his question after my post #2. According to new clarification, the path to the working folder comes to the script in finished Posix form from Alfred workflow. That is, on the contrary, the Posix path must initially be converted to an HFS path.
I would correct my post #2 in connection with the clarification received, but it makes little sense. Because the OP threw out the try blocks and said that he himself had already found the solution he needed.
It would be enough to replace the first code line in my post #2 with this:
set attachmentsPath to "/Users/beatrixwillius 1/Documents/Development/Mail Archiver issues from users" -- this comes from Alfred workflow
set attachmentsHFSPath to attachmentsPath as Posix file as text