Hi, guys,
I’ve got stuck with a very simple script. I need the “Preview” app to cycle through every file in a folder and open each file.
The script is below.
tell application “Finder” to set myFiles to every file of (choose folder)
repeat with aFile in myFiles
tell application “Preview”
open file myFiles
activate
end tell
end repeat
There is obviously something wrong with the script because the script does not work and return an error. I do not know where the error is.
I would appreciate if someone helped me.
Val
Try this, I haven’t run it myself but it should work.
tell application "Finder" to set myFiles to every file of (choose folder) as alias list
repeat with aFile in myFiles
tell application "Preview"
activate
open (contents of aFile)
end tell
end repeat
You were trying to use “myFiles” as a file but it is a list.
The bit about “(contents of aFile)” dereferences the variable “aFile” i.e. it looks like an alias to a file instead of “item 1 of myFiles” which won’t work. When you construct a loop like this one you iterate through a list by item # (usually) unless you “dereference” it.
Hi, Av8TnTek,
Thank you for your help. Now the script works.
This is a part of the bigger script that I am trying to develop. Instead of opening the files in a given directory, I will want to launch an email application and email them.
I will do as much as I could by myself but I most likely will need someone to look at the script that I produced.
Thank you again for your help.
Val
Hi, guys,
I’ve made the next step in my script which was to create one email per file in the folder.
I am trying to attach one file per email but I am again lost in basics with giving reference to the correct file.
Please take a look at the following code
tell application “Finder” to set myFiles to every file of (choose folder) as alias list
repeat with aFile in myFiles
tell application “Mail”
set aFile to make new outgoing message with properties {visible:true, subject:“My Subject”, content:“My Body”}
tell content of the aFile
make new attachment (aFile) at after last paragraph
end tell
end tell
end repeat
I am most likely misusing aFile again. Can someone point out to me what I am doing wrong?
Thank you,
Val
Hi, everyone,
I fixed the script. It is below. Now it works as intended.
tell application “Finder” to set myFiles to every file of (choose folder) as alias list
repeat with aFile in myFiles
set theAttachment to aFile
tell application “Mail”
set aFile to make new outgoing message with properties {visible:true, subject:“My Subject”, content:“My Body”}
tell content of aFile
make new attachment with properties {file name:theAttachment} at after last paragraph
end tell
end tell
end repeat
I made a bit of cleaning.
set theFolder to choose folder
tell application "Finder" to set myFiles to every file of theFolder as alias list
repeat with theFile in myFiles
tell application "Mail"
set aMessage to make new outgoing message with properties {visible:true, subject:"My Subject", content:"My Body"}
tell content of aMessage
make new attachment with properties {file name:theFile} at after last paragraph
end tell
end tell
end repeat
As you see, I moved choose folder out of the tell application “Finder” instruction because it belongs to Standard Additions.
In the loop I dropped one step, and modified the variables names so that they are more descriptive;
Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) vendredi 17 juin 2016 11:00:30
Hi, Yvan,
Thank you for editing the script. What would be the way to allow users to enter recipients’ addresses and subject via display dialog?
Thank you, again,
Val
Model: iMac OS X 10.11 El Capitan
Browser: Safari 601.6.17
Operating System: Other
You may try :
set theFolder to choose folder
tell application "Finder" to set myFiles to every file of theFolder as alias list
repeat with theFile in myFiles
tell application "System Events"
set isVisible to visible of theFile # Useful for me because here every files are displayed
end tell
if isVisible then
set thePath to theFile as text
set theSubject to text returned of (display dialog "Which subject for attachment : " & thePath default answer "My Subject : ")
set theDest to text returned of (display dialog "Which destination for attachment : " & thePath default answer "azerty@qsd.ok")
tell application "Mail"
set aMessage to make new outgoing message with properties {visible:true, subject:theSubject, content:"My Body" & linefeed & linefeed & linefeed}
tell aMessage
make new to recipient at end of to recipients with properties {address:theDest}
make new attachment with properties {file name:theFile} at after last paragraph of content
end tell
end tell
end if
end repeat
Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) vendredi 17 juin 2016 16:29:41
Hi, Yvan,
I have modified your script a little bit. I’ve taken user input for the recipient’s name and email address out of the repeat loop so that it could be entered once for all emails.
I tried to find a way to confirm via applescript that the attachment has been added to the outgoing email message but from what I found it seems to be not possible.
When I send pictures from the PhotoMechanic 5 application, if I choose to automatically send the email, most of the time emails are sent before the attachment is added to the message.
In this script it could be done via a delay command but the delay would depend on the size of the attachment. The bigger the attachment the more time it take to have it added to the message.
One more question, how could the user add more than one recipient?
Thank you for your help again.
Val
set theFolder to choose folder
tell application “Finder” to set myFiles to every file of theFolder as alias list
–set thePath to theFile as text
set theSubject to text returned of (display dialog "Which subject for attachment : " default answer "My Subject : ")
set theDest to text returned of (display dialog "Which destination for attachment : " default answer “azerty@qsd.ok”)
repeat with theFile in myFiles
tell application “System Events”
set isVisible to visible of theFile # Useful for me because here every files are displayed
end tell
if isVisible then
tell application "Mail"
set aMessage to make new outgoing message with properties {visible:true, subject:theSubject, content:"My Body" & linefeed & linefeed & linefeed}
tell aMessage
make new to recipient at end of to recipients with properties {address:theDest}
make new attachment with properties {file name:theFile} at after last paragraph of content
end tell
end tell
end if
end repeat
Hi Val. Welcome to MacScripter.
When posting AppleScript code, would you mind enclosing it between this site’s unique [applescript] and [/applescript] tags? There’s a button for them just above the text window on the posting page. They cause the code appear as in Av8TnTek’s and Yvan’s posts, with a clickable link which opens it in readers’ default script editors. Cheers.
set useChooseFolder to true # or false to fit your taste
if useChooseFolder then
set theFolder to choose folder
tell application "Finder" to set myFiles to every file of theFolder as alias list
else
set myFiles to choose file with multiple selections allowed
end if
set theSubject to text returned of (display dialog "Which subject for attachment : " default answer "My Subject : ")
set theDest to text returned of (display dialog "Which destination for attachment : " default answer "azerty@qsd.ok")
tell application "Mail"
set aMessage to make new outgoing message with properties {visible:true, subject:theSubject, content:"My Body" & linefeed & linefeed & linefeed}
tell aMessage
make new to recipient at end of to recipients with properties {address:theDest}
set nbAttachments to 0
repeat with theFile in myFiles
tell application "System Events"
set isVisible to visible of theFile # Useful for me because here every files are displayed
end tell # System Events
if isVisible then
make new attachment with properties {file name:theFile} at after last paragraph of content
repeat 10 times
if (count attachment) > nbAttachments then
set nbAttachments to nbAttachments + 1
exit repeat # If the attachment is really attached, exit this loop
tell me to delay 0.2 # delay before an other test
end if
end repeat
end if
end repeat
end tell # aMessage
end tell # Mail
I tested it with a folder containing three files whose sizes were : {3.4 Mbytes, 1.5 Mbytes, 1.4 Mbytes}
The variable useChooseFolder rules the way it get the files.
If useChooseFolder is set to true, use choose folder and attach the fioles embedded in this one
If useChooseFolder is set to false, use choose file with multiple selections allowed to do the job.
Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) vendredi 17 juin 2016 18:15:22
Here is an enhanced version which extract known mail addresses from Contacts.
use AppleScript version "2.3"
use scripting additions
use framework "Foundation"
set useChooseFolder to true # or false to fit your taste
if useChooseFolder then
set theFolder to choose folder
tell application "Finder" to set myFiles to every file of theFolder as alias list
else
set myFiles to choose file with multiple selections allowed
end if
set theSubject to text returned of (display dialog "Which subject for attachment : " default answer "My Subject : ")
set knownEmails to my extractKnownEmails() # Extract mail addresses from Contacts
set knownEmails to my sortAlist:knownEmails # Sort the list of mail addresses
set theDest to choose from list knownEmails # don't define the buttons so will use localized ones
if theDest is false then error "You pressed Cancel !"
# theDest is a list of one item !
set theDest to theDest's item 1# extracting explicitly the item 1 is not required here. Put it as a reminder.
tell application "Mail"
set aMessage to make new outgoing message with properties {visible:true, subject:theSubject, content:"My Body" & linefeed & linefeed & linefeed}
tell aMessage
make new to recipient at end of to recipients with properties {address:theDest}
set nbAttachments to 0
repeat with theFile in myFiles
tell application "System Events"
set isVisible to visible of theFile # Useful for me because here every files are displayed
end tell # System Events
if isVisible then
make new attachment with properties {file name:theFile} at after last paragraph of content
repeat 100 times
if (count attachment) > nbAttachments then
set nbAttachments to nbAttachments + 1
exit repeat # If the attachment is really attached, exit this loop
tell me to delay 0.2 # delay before an other test
end if
end repeat
end if
end repeat
end tell # aMessage
end tell # Mail
#=====
on extractKnownEmails()
set allEmails to {}
tell application "Contacts"
set thePeoples to every person
repeat with aPerson in thePeoples
tell aPerson
set theEmails to value of its every email
end tell
if theEmails is not {} then set allEmails to allEmails & theEmails
end repeat
end tell
return allEmails
end extractKnownEmails
#=====
on sortAlist:aList
set anArray to current application's NSArray's arrayWithArray:aList
return (anArray's sortedArrayUsingSelector:"localizedCaseInsensitiveCompare:") as list
end sortAlist:
#=====
Thanks to ASObjC (and of course, thanks to Shane STANLEY) we may use an efficient script code requiring few typing
CAUTION : Contacts is one of these odd applications which must be open to allow Script Editor to compile :rolleyes:
The next version extract the name and the mail address of the target.
use AppleScript version "2.3"
use scripting additions
use framework "Foundation"
set useChooseFolder to true # or false to fit your taste
if useChooseFolder then
set theFolder to choose folder
tell application "Finder" to set myFiles to every file of theFolder as alias list
else
set myFiles to choose file with multiple selections allowed
end if
set theSubject to text returned of (display dialog "Which subject for attachment : " default answer "My Subject : ")
set knownEmails to my extractKnownEmails() # Extract mail addresses from Contacts
set knownEmails to my sortAlist:knownEmails # Sort the list of mail addresses
set theDest to choose from list knownEmails # don't define the buttons so will use localized ones
if theDest is false then error "You pressed Cancel !"
# Here theDest is a list of one item. We must extract its 1st item explicitly before splitting it !
set {theName, theAddress} to my decoupe(theDest's item 1, tab)
tell application "Mail"
set aMessage to make new outgoing message with properties {visible:true, subject:theSubject, content:"My Body" & linefeed & linefeed & linefeed}
tell aMessage
make new to recipient at end of to recipients with properties {name:theName, address:theAddress}
set nbAttachments to 0
repeat with theFile in myFiles
tell application "System Events"
set isVisible to visible of theFile # Useful for me because here every files are displayed
end tell # System Events
if isVisible then
make new attachment with properties {file name:theFile} at after last paragraph of content
repeat 100 times
if (count attachment) > nbAttachments then
set nbAttachments to nbAttachments + 1
exit repeat # If the attachment is really attached, exit this loop
tell me to delay 0.2 # delay before an other test
end if
end repeat
end if
end repeat
end tell # aMessage
end tell # Mail
#=====
on extractKnownEmails()
set allTargets to {}
tell application "Contacts"
set thePeoples to every person
repeat with aPerson in thePeoples
tell aPerson
set theName to its name
set theEmails to value of its every email
end tell
if theEmails is not {} then
repeat with anEmail in theEmails
set end of allTargets to theName & tab & anEmail
end repeat
end if
end repeat
end tell
return allTargets
end extractKnownEmails
#=====
on sortAlist:aList
set anArray to current application's NSArray's arrayWithArray:aList
return (anArray's sortedArrayUsingSelector:"localizedCaseInsensitiveCompare:") as list
end sortAlist:
#=====
on decoupe(t, d)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
#=====
Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) samedi 18 juin 2016 11:30:56