Mail Can't continue for a AppleScript function
Try "my isImageFileName". It's not Mail, just a quirk of AppleScript.
Using AppleScript with Apple Events in macOS - Script not working
The problem with this code — which is an incredibly un-obvious problem, mind you — is that you're using code meant to run a script handler (a method or subroutine) to try to run the full script. One of the oddnesses of Obj-C's AppleScript classes is that there is no easy way to run a script with parameters, so the workaround is to enclose the code to be executed within a script handler, and use an Apple Event that calls that handler. To make your code work, you'll do something like the following...
First, change the script so that the code is in a handler:
var script: NSAppleScript = {
let script = NSAppleScript(source: """
-- This is our handler definition
on sendMyEmail(theSubject, theContent, recipientName, recipientAddress, attachmentPath)
tell application "Mail"
-- Create an email
set outgoingMessage to make new outgoing message ¬
with properties {subject:theSubject, content:theContent, visible:true}
-- Set the recipient
tell outgoingMessage
make new to recipient ¬
with properties {name:recipientName, address:recipientAddress}
make new attachment with properties {file name:POSIX file attachmentPath}
-- Mail.app needs a moment to process the attachment, so...
delay 1
-- Send the message
send
end tell
end tell
end sendMyEmail
"""
)!
Then alter the Apple Event you construct so that it passes the parameters and calls the handler we just defined:
func runScript() {
let parameters = NSAppleEventDescriptor.list()
parameters.insert(NSAppleEventDescriptor(string: "Some Subject"), at: 0)
parameters.insert(NSAppleEventDescriptor(string: "Some content of the email"), at: 0)
parameters.insert(NSAppleEventDescriptor(string: "Some Name"), at: 0)
parameters.insert(NSAppleEventDescriptor(string: "someone@example.com"), at: 0)
parameters.insert(NSAppleEventDescriptor(string: attachmentFileURL.path), at: 0)
let event = NSAppleEventDescriptor(
eventClass: AEEventClass(kASAppleScriptSuite),
eventID: AEEventID(kASSubroutineEvent),
targetDescriptor: nil,
returnID: AEReturnID(kAutoGenerateReturnID),
transactionID: AETransactionID(kAnyTransactionID)
)
// this line sets the name of the target handler
event.setDescriptor(NSAppleEventDescriptor(string: "sendMyEmail"), forKeyword: AEKeyword(keyASSubroutineName))
// this line adds the parameter list we constructed above
event.setDescriptor(parameters, forKeyword: AEKeyword(keyDirectObject))
var error: NSDictionary? = nil
_ = self.script.executeAppleEvent(event, error: &error) as NSAppleEventDescriptor?
print ("runScript",self.script)
}
}
If you don't need to pass parameters, you could run the script directly using script.executeAndReturnError(&error)
, but if you need to pass parameters, you'll need to use this 'handler' approach.
AppleScript handler not working when called from within tell Finder block
When you are calling a handler from within a tell block or other handler, you need to specify "my", otherwise it is looking for that command in the Finder dicitonary.
my checkForRebels(tieFighter, starDestroyer)
From the Applescript Language Guide Calling Handlers in a tell Statement:
To call a handler from within a tell statement, you must use the
reserved words of me or my to indicate that the handler is part of the
script and not a command that should be sent to the target of the tell
statement.
Applescript - drag/drop file attachment to open new email
Was given this on MacScripter / AppleScript | OS X and it works fine:
property Myrecipient : "some email"
property mysubject : "some subject"
property EmailBody : "some body text"
on run
tell application "Finder"
set sel to (get selection)
end tell
new_mail(sel)
end run
on open droppedFiles
new_mail(droppedFiles)
end open
on new_mail(theFiles)
tell application "Mail"
set newMessage to make new outgoing message with properties {visible:true, subject:mysubject, content:EmailBody}
tell newMessage
make new to recipient with properties {address:Myrecipient}
tell content
repeat with oneFile in theFiles
make new attachment with properties {file name:oneFile as alias} at after the last paragraph
end repeat
end tell
end tell
activate
end tell
end new_mail
can i add several attachments from subfolders to Mail?
You must pass the variables as parameters in the handler :
1- (item i of emailList)
: i and emailList is not defined in the handler.
2- {file name:FileList}
: FileList is not defined in the handler, file name must be a path of type alias
or string
, not a list of path.
set myFiles to entire contents of myFolder
: the myfolder variable is an integer
, entire contents
will contains the folders and files, if the folder doesn't contains subfolders, entire contents
is useless, use files of xFolder
.
The rest is okay, but contains unnecessary lines.
Here is the script:
with timeout of 600 seconds
-- Liste: Alle Empfänger
tell application "Contacts"
set emailList to value of email 1 of every person of group "Test"
end tell
-- Liste fuer die Übergabe alphabetisch sortieren
set otid to AppleScript's text item delimiters
set AppleScript's text item delimiters to {linefeed}
do shell script "echo " & (quoted form of (emailList as string)) & " | sort -f"
set emailList to (paragraphs of the result)
set AppleScript's text item delimiters to otid
-- Liste: Alle Subfolder
activate
set mainfolder to choose folder "select a folder"
tell application "Finder" to set folderList to folders of mainfolder
-- Sicherheits-Check
set count1 to count folderList
if count1 is not equal to (count emailList) then
display dialog "Houston, we have a problem:" & return & "Die beiden Listen sind nicht gleich lang..." buttons {"ok"} cancel button "ok" with icon 2
end if
end timeout
-- grab attachments and send mail
repeat with i from 1 to count1
try
tell application "Finder" to set myFiles to (files of entire contents of (item i of folderList)) as alias list
my processfolder(myFiles, item i of emailList)
end try -- no error on empty folder
end repeat
display dialog (count1 as string) & " Nachrichten verschickt."
on processfolder(tFiles, theAddress)
tell application "Mail"
activate
tell (make new outgoing message at end of outgoing messages with properties {visible:true, subject:"Subjectheader", content:("email body" & linefeed & " ")})
make new to recipient at end of to recipients with properties {address:theAddress}
tell content to repeat with tFile in tFiles
make new attachment with properties {file name:tFile} at after last paragraph
make new character with data linefeed at after last paragraph
end repeat
send
end tell
end tell
end processfolder
Applescript Mail - Script won't get past ''on perform mail action with messages theMessages for rule theRule
The do shell script
line must be inside the handler. And you have to quote all parameters and coerce theDate
to text.
Finally there is a typo: It's date received
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
repeat with theMessage in theMessages
set theDate to date received of theMessage
set theText to content of theMessage
set theSender to sender of theMessage
do shell script "php -q /Users/kaartendrukkerijmacmini/Dropbox/Technische_ontwikkeling/PHP_Webhook_approval_post.ctp " & quoted form of (theDate as text) & space & quoted form of theText & space & quoted form of theSender
end repeat
end perform mail action with messages
end using terms from
If the handler is not called at all the issue is somewhere else.
Issues with On Open Applescript handler in Yosemite
This bizarre effect is due to quarantined files. Quarantined files can be checked with the command:
xattr -p com.apple.quarantine *
Depending on the sort order of the quarantined/non-quarantined files, it will separately execute the "on open" handler for each group (be it quarantined or non-quarantined): e.g., 1 - quarantined, 4 - non-quarantined, 3 - quarantined. You'll notice there are two groups of quarantined files being submitted in this example, and that's because of how that particular list was sorted and submitted to the on open handler.
This behavior is rather surprising, and I've submitted it as a bug report to Apple. The quarantine attribute can be removed with this command:
sudo xattr -dr com.apple.quarantine *
to show the correct number of files. Also, see the Applescript trick above by regulus6633 for a clever workaround.
Related Topics
How to Programmatically Change The Xdr Display Reference Mode (Aka Preset) on Macos
My UIcollectionview Does Not Scroll Smoothly Using Swift
How to Present Ads During Certain Scenes in Sprite-Kit
Swiftsupport Folder Not Included in The IPA When Generating Build from Script
Nskeyedunarchiver Decodeobjectforkey: Cannot Decode Object of Class for Key (Ns.Objects)
Nsextensionpointidentifier Error Only on Real Device
Do Swift Hashable Protocol Hash Functions Need to Return Unique Values
Xcode8 Beta 6 - Urlsession with Completionhandler Argument Not Working
How to Implement Doubly Linked List in Swift
How to Create Viewcontrollers Without Storyboard and Set One as Delegate of The Other One
Reload Tableview After Clicking The Button Present Inside One of The Tableview Cell in Swift
Swiftui Change on Multilevel Children Published Object Change
Uitableviewcell Subclass Wrong Image in Cell or Old Image Bug
Why Does a Function Have Long-Term Write Access to All of Its In-Out Parameters
Applying Impulses in Spritekit
Why Is Swift Giving Me Inaccurate Floating Point Arithmetic Results
Adding Items to The Dock Menu from My View Controller in My Cocoa App