Trying to write a subroutine that will create a new folder and then open it. This is where I am at present and get the error message "unable to read the dictionary of the application or extensions because it is not scriptable.
set destinationFolder to choose folder
set nameFolder to text returned of (display dialog "Name New Folder " buttons {"OK", "None"} default button "OK" default answer "")
tell application "Finder" to make new folder at destinationFolder with properties {name:nameFolder}
set destinationFolder to POSIX path of (destinationFolder) & nameFolder
open destinationFolder
Any suggestions greatly appreciated seems like this should be easy to do but I am probably missing the obvious.
set destinationFolder to choose folder
set nameFolder to text returned of (display dialog "Name New Folder " buttons {"OK", "None"} default button "OK" default answer "")
tell application "Finder"
set createdFolder to make new folder at destinationFolder with properties {name:nameFolder}
open createdFolder
end tell
or
set destinationFolder to choose folder
set nameFolder to text returned of (display dialog "Name New Folder " buttons {"OK", "None"} default button "OK" default answer "")
tell application "Finder" to make new folder at destinationFolder with properties {name:nameFolder}
set destinationFolder to POSIX path of (destinationFolder) & nameFolder
do shell script "open " & quoted form of destinationFolder
Thanks again that of course worked perfectly but I was actually trying to solve another problem. You may not recall but you have helped me before on a project I have been working on for ages.
I have been trying to develop a script that saves selected e-mails as PDF’s and there attachments. Using the subject and initials of the snider (or first part of email address if no name) as the file name. The way attachments are saved from Mail means the names cannot be changed or that appears to be the case as I see it. Consequently it is not always obvious which attachments belong to which PDF. The solution I thought would work was to create a folder with the same name as the subject and put the attachments in there.
I aim including the entire scrip but it all works accept when I try to save the attachments in the handler SaveAttach(). Before I tried to add the creation of the new folder it worked OK. The error occurs when i try and set the value of the text field to the POSIX path of the destination folder .
--Introduction to email Processing under development 09122011
global destinationFolder, Subject_, Sender_, msgcount, theMsg, Init
set Init to {}
set destinationFolder to ""
repeat
tell application "Mail"
activate "Mail" --ensure app to front
set msgcount to count of selection
set check to button returned of (display dialog "Continue Processing emails " buttons {"Yes", "No"} default button 1)
if check = "No" then
exit repeat
else
if msgcount > 1 then
display dialog "More than one message selected go back select one and rerun"
exit repeat
else
set theMsg to selection
set theMsg to item 1 of theMsg
set theAttachmentCount to count every mail attachment of theMsg
my ProcessMail() --When Script run process first message
my FileasPDF() --Calls handler to process the save part of routine
if the theAttachmentCount > 0 then
my SaveAttach()
end if
delay 5 --Needed other wise possible that save part of routine not completed before next sub is called
--set dxf to count of characters of POSIX path of destinationFolder
--display dialog (destinationFolder as string) & " characters " & dxf
my NextMessage() --Move cursor down one message
end if
end if
end tell
end repeat
--------------------
on NextMessage() --Moves the selection to next email in box
tell application "Mail"
activate "mail"
tell application "System Events"
tell process "mail"
key code 125 --moves arrow key down in selected application 123 left 124 right 126 up
end tell
end tell
end tell
end NextMessage
-----------------
on ProcessMail()
--set destinationFolder to ""
tell application "Mail"
activate "Mail" --ensure app to front
delay 0.05
set Subject_ to subject of item 1 of theMsg
if (offset of "RE:" in Subject_) as integer > 0 then set Subject_ to characters 4 through -1 of Subject_ as string --Strips out Re: when subject not changed in email
set Sender_ to sender of item 1 of theMsg
--Determine if email in or out
if (offset of "mitchbvi@me.com" in Sender_) as integer > 0 ¬
or (offset of "mitchvail@me.com" in Sender_) as integer > 0 ¬
or (offset of "mitchbvi@mac.com" in Sender_) as integer > 0 ¬
or (offset of "mitchvail@mac.com" in Sender_) as integer > 0 ¬
or (offset of "mitch@surfbvi.com" in Sender_) as integer > 0 then
set Sender_ to "" --Sender is me
set Recipient_ to name of first recipient of item 1 of theMsg
set Subject_ to Subject_ & " to " & Recipient_
set Status to "Outward"
else --must be inward email
set Sender_ to extract name from Sender_
if (offset of "@" in Sender_) as integer > 0 then --test if name has been extracted if not switches to 1st part email address
set brkpos to offset of "@" in Sender_
set Sender_ to characters 1 through (brkpos - 1) of Sender_
else if (count of words in Sender_) > 1 then
repeat with i from 1 to count of words in Sender_
set Init to Init & first character of word i of Sender_
end repeat
set Sender_ to Init
end if
set Subject_ to Subject_ & " From " & Sender_
set Status to "Inward"
set Recipient_ to ""
end if
set theMsgCount to count every mail attachment of theMsg --counts attachments if any
set Subject_ to text returned of (display dialog Status & " email from " & return & Sender_ & return ¬
& "Proposed file name is in the box " & return & ¬
"Select OK or start typing to erase and change " & return & ¬
"and then Select OK" buttons {"OK", "Change"} default button "OK" default answer (Subject_))
set Subject_ to my FixFileName(Subject_) --Calls handler to strip bad characters
--Get folder to save email in
try
if (count of characters of POSIX path of destinationFolder) > 0 then --Cannot count characters in path returned so have to switch to POSIX path and cannot get POSIX Path of a null string hence try to catch the error
if button returned of (display dialog ("Folder Currently selected " & POSIX path of destinationFolder as string) ¬
& return buttons {"OK", "Change"} default button 1) = "Change" then
set destinationFolder to choose folder
end if
end if
on error
display dialog "Path not yet selected"
set destinationFolder to (choose folder)
end try
set Subject_ to my getEmptyPath((destinationFolder as string), Subject_) --Calls handler to check if file exists in destinationFolder
end tell
end ProcessMail
------------------------------------
on FixFileName(Subject_) --Deletes characters that cannot be used in file names
set fixed_string to {}
set bad_char to {":", "/"}
repeat with c from 1 to (count every character in Subject_)
if bad_char contains (character c of Subject_) then
set end of fixed_string to " "
else
set end of fixed_string to (character c of Subject_)
end if
end repeat
fixed_string as string
set Subject_ to fixed_string
end FixFileName
---------------------------------------
-- See if a file with the passed name exists
-- If so, add an integer to the name, and check again
-- Keep checking until an unused name is found
-- Parameters: path of container as text
-- file name, no extension
-- This is only intended to save mail messages as PDF's so that extension is added in the handler
-- Returns: updated file name
on getEmptyPath(destinationFolder, Subject_)
set filePath to destinationFolder & Subject_
set Ext to ".PDF" --only checking for file type PDf as that is how the message is being saved
tell application "Finder"
if exists file (filePath & Ext) then
set x to 1 -- start counting
repeat
set cFilePath to filePath & " " & x & Ext
if exists file cFilePath then -- found empty spot?
set x to x + 1 -- no, keep counting
else -- yes, leave
exit repeat
end if
end repeat
return Subject_ & " " & x -- this name is safe to use
else
return Subject_ -- use original name
end if
end tell
end getEmptyPath
--------------------------------
on FileasPDF()
tell application "System Events"
tell process "Mail"
keystroke "p" using command down
repeat until exists sheet 1 of window 1
delay 0.2
end repeat
tell sheet 1 of window 1
click menu button "PDF"
repeat until exists menu 1 of menu button "PDF"
delay 0.02
end repeat
click menu item "Save as PDF." of menu 1 of menu button "PDF"
end tell
repeat until exists window "Save"
delay 0.2
end repeat
tell window "Save"
keystroke "g" using {command down, shift down}
repeat until exists sheet 1
delay 0.2
end repeat
tell sheet 1
--Subject_Added Peter to test how to pass file name
set value of text field 1 to POSIX path of destinationFolder & "/" & Subject_
click button "Go"
end tell
repeat while exists sheet 1
delay 0.2
end repeat
click button "Save"
end tell
end tell
end tell
end FileasPDF
---------------------
on SaveAttach()
set Subject_ to Subject_ as string
tell application "Finder" to make new folder at destinationFolder with properties {name:Subject_}
set destinationFolder to destinationFolder & Subject_
tell application "System Events"
tell process "Mail"
--here's where your script went slightly wrong. The three periods after "Save Attachments" are part of the name
click menu item "Save Attachments." of menu "File" of menu bar 1
tell window 1
repeat until exists sheet 1
delay 0.2
end repeat
keystroke "g" using {command down, shift down} --open go to folder sheet
repeat until exists sheet 1 of sheet 1
delay 0.2
end repeat
tell sheet 1 of sheet 1
set value of text field 1 to POSIX path of destinationFolder --the go to folder text field
click button "Go"
end tell
repeat while exists sheet 1 of sheet 1
delay 0.2
end repeat
tell sheet 1 --this automatically switches to the first sheet again
click button "Save" -- the button on the first sheet
end tell
end tell
end tell
end tell
end SaveAttach
Hope this all makes sense and once again my sincere thanks.
destinationFolder is an alias specifier, it cannot concatenated with a string path component
I recommend
on SaveAttach()
-- set Subject_ to Subject_ as string Subject is presumably always a string
tell application "Finder" to set createdFolder to make new folder at destinationFolder with properties {name:Subject_}
set createdFolder to createdFolder as text
tell application "System Events"
tell process "Mail"
--here's where your script went slightly wrong. The three periods after "Save Attachments" are part of the name
click menu item "Save Attachments." of menu "File" of menu bar 1
tell window 1
repeat until exists sheet 1
delay 0.2
end repeat
keystroke "g" using {command down, shift down} --open go to folder sheet
repeat until exists sheet 1 of sheet 1
delay 0.2
end repeat
tell sheet 1 of sheet 1
set value of text field 1 to POSIX path of createdFolder --the go to folder text field
click button "Go"
end tell
repeat while exists sheet 1 of sheet 1
delay 0.2
end repeat
tell sheet 1 --this automatically switches to the first sheet again
click button "Save" -- the button on the first sheet
end tell
end tell
end tell
end tell
end SaveAttach
Note: Anyway you will get an error if the subfolder createdFolder already exists
Thanks Stefan…However cannot get it to work. I replaced my SaveAttach() with your code and I get the error
Finder got an error: Can’t make {" ", “F”, “r”, “o”, “m”, " ", “L”, “S”, " ", 1} into type Unicode text.that I cannot make into unicode text.
If I reactivate the setting the Subject_ to a string I then get an error
Can’t get POSIX path of “Macintosh HD:Users:mitch:Desktop:Mailtest2: From LS:”.
interestingly it has however created the folder, and when I get this error the Go to the folder: drop down box has appeared with cancel and go buttons.
Stefan should not get error with createdfolder as it is the same as file name and that is set to change by adding a number to the Subject if it already exists.
OK, the variable Subject_ is not a string, it’s a list of characters, created in your FixFileName() handler.
Maybe this solves the problem.
coerce the list to string at the end of the handler by setting text item delimiters to a reliable value
on FixFileName(Subject_) --Deletes characters that cannot be used in file names
set fixed_string to {}
set bad_char to {":", "/"}
repeat with c from 1 to (count every character in Subject_)
if bad_char contains (character c of Subject_) then
set end of fixed_string to " "
else
set end of fixed_string to (character c of Subject_)
end if
set {TID, text item delimiters} to {text item delimiters, ""}
set Subject_ to fixed_string as text
set text item delimiters to TID
end repeat
end FixFileName
The second error message reports that the 1st character of Subject_ is a space character.
You should filter leading spaces too
Had to move the TID block outside the repeat otherwise it zero’d the string on the first iteration.
on FixFileName(Subject_) --Deletes characters that cannot be used in file names
set fixed_string to {}
set bad_char to {":", "/"}
repeat with c from 1 to (count every character in Subject_)
if bad_char contains (character c of Subject_) then
set end of fixed_string to " "
else
set end of fixed_string to (character c of Subject_)
end if
end repeat
set {TID, text item delimiters} to {text item delimiters, ""}
set Subject_ to fixed_string as text
set text item delimiters to TID
end FixFileName
But i still get the POSIX error but with twist it has created a new folder but it is untitled has not given it a name!! Also when it saved the message it gave it the name Mail Message .PDF
Can’t get POSIX path of “Macintosh HD:Users:mitch:Desktop:Mailtest2:untitled folder:”.
it then goes into a seemingly endless loop here is bait of it
click button “Save” of window “Save” of process “Mail”
→ button “Save” of window “Save” of application process “Mail”
end tell
tell application “Finder”
make new folder at alias “Macintosh HD:Users:mitch:Desktop:Mailtest2:” with properties {name:{“”}}
→ folder “untitled folder 4” of folder “Mailtest2” of folder “Desktop” of folder “mitch” of folder “Users” of startup disk
get folder “untitled folder 4” of folder “Mailtest2” of folder “Desktop” of folder “mitch” of folder “Users” of startup disk
→ “Macintosh HD:Users:mitch:Desktop:Mailtest2:untitled folder 4:”
end tell
tell application “System Events”
click menu item “Save Attachments.” of menu “File” of menu bar 1 of process “Mail”
→ menu item “Save Attachments.” of menu “File” of menu bar item “File” of menu bar 1 of application process “Mail”
exists sheet 1 of window 1 of process “Mail”
→ true
keystroke “g” using {command down, shift down}
exists sheet 1 of sheet 1 of window 1 of process “Mail”
→ false
exists sheet 1 of sheet 1 of window 1 of process “Mail”
→ false
exists sheet 1 of sheet 1 of window 1 of process “Mail”
→ false
Sorry about this thanks
Peter
Very Strange I just moved the TID block to the start of SaveAttach() and it created the correct Folder saved the message with correct name but still gives me the POSIX path error. Yet if I click Go on the drop down box and then save it post the attachment int he folder created.
the changed handler returns the reset value of text item delimiters “ usually {“”} “ which is assigned to the variable Subject_. An empty string (or list) causes the folder to be created with generic name
Change this line, the global Subject_ preserves the value anyway
my FixFileName(Subject_) --Calls handler to strip bad characters
on FixFileName(Subject_) --Deletes characters that cannot be used in file names
set fixed_string to {}
set bad_char to {":", "/"}
repeat with c from 1 to (count every character in Subject_)
if bad_char contains (character c of Subject_) then
set end of fixed_string to " "
else
set end of fixed_string to (character c of Subject_)
end if
end repeat
set {TID, text item delimiters} to {text item delimiters, ""}
set Subject_ to fixed_string as text
set text item delimiters to TID
end FixFileName
Do you use my code (variable createdFolder) in the saveAttach() handler?
Did that and yes I am using your SaveAttach and I am still getting
Can’t get POSIX path of "Macintosh HD:Users:mitch:Desktop:Mailtest2: From LS:
As I mentioned before the drop down box appears if I click go then save it puts the attachment in the folder just created but I still have the error message!!
Here is the whole script as it now is.
--Introduction to email Processing under development 09122011
global destinationFolder, Subject_, Sender_, msgcount, theMsg, Init
set Init to {}
set destinationFolder to ""
repeat
tell application "Mail"
activate "Mail" --ensure app to front
set msgcount to count of selection
set check to button returned of (display dialog "Continue Processing emails " buttons {"Yes", "No"} default button 1)
if check = "No" then
exit repeat
else
if msgcount > 1 then
display dialog "More than one message selected go back select one and rerun"
exit repeat
else
set theMsg to selection
set theMsg to item 1 of theMsg
set theAttachmentCount to count every mail attachment of theMsg
my ProcessMail() --When Script run process first message
my FileasPDF() --Calls handler to process the save part of routine
if the theAttachmentCount > 0 then
my SaveAttach()
end if
delay 5 --Needed other wise possible that save part of routine not completed before next sub is called
--set dxf to count of characters of POSIX path of destinationFolder
--display dialog (destinationFolder as string) & " characters " & dxf
my NextMessage() --Move cursor down one message
end if
end if
end tell
end repeat
--------------------
on NextMessage() --Moves the selection to next email in box
tell application "Mail"
activate "mail"
tell application "System Events"
tell process "mail"
key code 125 --moves arrow key down in selected application 123 left 124 right 126 up
end tell
end tell
end tell
end NextMessage
-----------------
on ProcessMail()
--set destinationFolder to ""
tell application "Mail"
activate "Mail" --ensure app to front
delay 0.05
set Subject_ to subject of item 1 of theMsg
if (offset of "RE:" in Subject_) as integer > 0 then set Subject_ to characters 4 through -1 of Subject_ as string --Strips out Re: when subject not changed in email
set Sender_ to sender of item 1 of theMsg
--Determine if email in or out
if (offset of "mitchbvi@me.com" in Sender_) as integer > 0 ¬
or (offset of "mitchvail@me.com" in Sender_) as integer > 0 ¬
or (offset of "mitchbvi@mac.com" in Sender_) as integer > 0 ¬
or (offset of "mitchvail@mac.com" in Sender_) as integer > 0 ¬
or (offset of "mitch@surfbvi.com" in Sender_) as integer > 0 then
set Sender_ to "" --Sender is me
set Recipient_ to name of first recipient of item 1 of theMsg
set Subject_ to Subject_ & " to " & Recipient_
set Status to "Outward"
else --must be inward email
set Sender_ to extract name from Sender_
if (offset of "@" in Sender_) as integer > 0 then --test if name has been extracted if not switches to 1st part email address
set brkpos to offset of "@" in Sender_
set Sender_ to characters 1 through (brkpos - 1) of Sender_
else if (count of words in Sender_) > 1 then
repeat with i from 1 to count of words in Sender_
set Init to Init & first character of word i of Sender_
end repeat
set Sender_ to Init
end if
set Subject_ to Subject_ & " From " & Sender_
set Status to "Inward"
set Recipient_ to ""
end if
set theMsgCount to count every mail attachment of theMsg --counts attachments if any
set Subject_ to text returned of (display dialog Status & " email from " & return & Sender_ & return ¬
& "Proposed file name is in the box " & return & ¬
"Select OK or start typing to erase and change " & return & ¬
"and then Select OK" buttons {"OK", "Change"} default button "OK" default answer (Subject_))
my FixFileName(Subject_) --Calls handler to strip bad characters
--Get folder to save email in
try
if (count of characters of POSIX path of destinationFolder) > 0 then --Cannot count characters in path returned so have to switch to POSIX path and cannot get POSIX Path of a null string hence try to catch the error
if button returned of (display dialog ("Folder Currently selected " & POSIX path of destinationFolder as string) ¬
& return buttons {"OK", "Change"} default button 1) = "Change" then
set destinationFolder to choose folder
end if
end if
on error
display dialog "Path not yet selected"
set destinationFolder to (choose folder)
end try
set Subject_ to my getEmptyPath((destinationFolder as string), Subject_) --Calls handler to check if file exists in destinationFolder
end tell
end ProcessMail
------------------------------------
on FixFileName(Subject_) --Deletes characters that cannot be used in file names
set fixed_string to {}
set bad_char to {":", "/"}
repeat with c from 1 to (count every character in Subject_)
if bad_char contains (character c of Subject_) then
set end of fixed_string to " "
else
set end of fixed_string to (character c of Subject_)
end if
end repeat
set {TID, text item delimiters} to {text item delimiters, ""}
set Subject_ to Subject_ as text
--display dialog "End fix file name " & Subject_
set text item delimiters to TID
end FixFileName
---------------------------------------
-- See if a file with the passed name exists
-- If so, add an integer to the name, and check again
-- Keep checking until an unused name is found
-- Parameters: path of container as text
-- file name, no extension
-- This is only intended to save mail messages as PDF's so that extension is added in the handler
-- Returns: updated file name
on getEmptyPath(destinationFolder, Subject_)
set filePath to destinationFolder & Subject_
set Ext to ".PDF" --only checking for file type PDf as that is how the message is being saved
tell application "Finder"
if exists file (filePath & Ext) then
set x to 1 -- start counting
repeat
set cFilePath to filePath & " " & x & Ext
if exists file cFilePath then -- found empty spot?
set x to x + 1 -- no, keep counting
else -- yes, leave
exit repeat
end if
end repeat
return Subject_ & " " & x -- this name is safe to use
else
return Subject_ -- use original name
end if
end tell
end getEmptyPath
--------------------------------
on FileasPDF()
tell application "System Events"
tell process "Mail"
keystroke "p" using command down
repeat until exists sheet 1 of window 1
delay 0.2
end repeat
tell sheet 1 of window 1
click menu button "PDF"
repeat until exists menu 1 of menu button "PDF"
delay 0.02
end repeat
click menu item "Save as PDF." of menu 1 of menu button "PDF"
end tell
repeat until exists window "Save"
delay 0.2
end repeat
tell window "Save"
keystroke "g" using {command down, shift down}
repeat until exists sheet 1
delay 0.2
end repeat
tell sheet 1
--Subject_Added Peter to test how to pass file name
set value of text field 1 to POSIX path of destinationFolder & "/" & Subject_
click button "Go"
end tell
repeat while exists sheet 1
delay 0.2
end repeat
click button "Save"
end tell
end tell
end tell
end FileasPDF
---------------------
on SaveAttach()
--set Subject_ to Subject_ as string --Subject is presumably always a string
tell application "Finder" to set createdFolder to make new folder at destinationFolder with properties {name:Subject_}
set createdFolder to createdFolder as text
tell application "System Events"
tell process "Mail"
--here's where your script went slightly wrong. The three periods after "Save Attachments" are part of the name
click menu item "Save Attachments." of menu "File" of menu bar 1
tell window 1
repeat until exists sheet 1
delay 0.2
end repeat
keystroke "g" using {command down, shift down} --open go to folder sheet
repeat until exists sheet 1 of sheet 1
delay 0.2
end repeat
tell sheet 1 of sheet 1
set value of text field 1 to POSIX path of createdFolder --the go to folder text field
click button "Go"
end tell
repeat while exists sheet 1 of sheet 1
delay 0.2
end repeat
tell sheet 1 --this automatically switches to the first sheet again
click button "Save" -- the button on the first sheet
end tell
end tell
end tell
end tell
end SaveAttach
Thought I knew how to set delimiters but for leading spaces I do dot. I moved to message that had a subject and still get the error.
Can’t get POSIX path of “Macintosh HD:Users:mitch:Desktop:Mailtest2:Fwd/ IMMEDIATE ACTION REQUIRED - HALL 1-33 From DS:”.
However I am now not sure my FixFileName handler is working because as you can see the knew folder referred to above (same name as file) has a “/” in it what do you think.
I told you I was dumb have now fixed that and delete the parameter Subject_ . The file name has had the “/” deleted but I still get the POSIX error…
Can’t get POSIX path of “Macintosh HD:Users:mitch:Desktop:Mailtest2:Fwd IMMEDIATE ACTION REQUIRED - HALL 1-33 From DS:”
I tried to lengthen the delay and as I mentioned before if I click on the Go in the drop down dialog box and then save it puts the attachment in the correct folder.
So it is saying it cannot get the POSIX path yet the box has dropped down what do you make of that?
Thank you
Peter
PS
Stefan
Have to go out please excuse me if I do not reply straight away.