Saturday, November 27, 2021

#1 2021-11-19 12:37:01 am

bwill
Member
Registered: 2019-07-17
Posts: 10

Get reference to folder

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

Applescript:

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?

Offline

 

#2 2021-11-19 01:13:27 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2154

Re: Get reference to folder

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:

Applescript:


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

Last edited by KniazidisR (2021-11-19 01:16:40 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#3 2021-11-19 01:51:57 am

bwill
Member
Registered: 2019-07-17
Posts: 10

Re: Get reference to folder

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:

Applescript:

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

Thanks!

Offline

 

#4 2021-11-19 06:28:28 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11755
Website

Re: Get reference to folder

It's much easier to get the POSIX path from an HFS path than the other way round

Applescript:

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

Last edited by StefanK (2021-11-19 06:28:57 am)


regards

Stefan

Offline

 

#5 2021-11-19 09:04:51 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2154

Re: Get reference to folder

@StefanK,

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:

Applescript:


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

Last edited by KniazidisR (2021-11-19 09:24:53 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)