Applescript and empty mail signature / input in value list - applescript

I have two questions with my applescript. The script is supposed to send a dropped file as attachment by email and asking the object of the mail from a list.
The content of the message MUST be empty.
1) How to set an "empty" email signature because the content of my mail should be empty. I receive an error code "error in mail impossible to solve signature..."
2) I wish that the user can modify the value list {"00111111111111-number1, "0011111111111-number2"...} and add more numbers. What is the best approach to do this ?
Thanks very much in advance for your suggestions.
property theSubject : "subject"
property theNumber : ""
property theContent : ""
property theSignature : "none"
property onRun : ""
on run
tell application "Finder"
set sel to (get selection)
end tell
set onRun to 1
new_mail(sel)
end run
on open droppedFiles
new_mail(droppedFiles)
end open
on new_mail(theFiles)
set chosen to choose from list {"0011111111111-number1", "0011111111111-number2"} with prompt "Thanks to select"
if chosen is false then return "" -- in case of 'Cancel' return empty string
set theNumber to text 1 thru 13 of (item 1 of chosen) -- as chosen returns a list by default it must be flattened
tell application "Mail"
set newMessage to make new outgoing message with properties {visible:true, subject:theNumber}
tell newMessage
make new to recipient with properties {address:faxboxEmail}
if onRun < 1 then
make new attachment with properties {file name:theFiles as alias} at after last paragraph
end if
set the content to theContent
set message signature of newMessage to signature theSignature
end tell
activate
if onRun < 1 then
send
end if
end tell
end new_mail

to add new item in the list, may be this could help you ?
set BaseList to {"0011111111111-number1", "0011111111111-number2"}
set CList to BaseList & {"Add new item"}
set chosen to choose from list CList with prompt "Thanks to select"
if chosen is false then return "" -- in case of 'Cancel' return empty string
if "Add new item" is in chosen then
set OKNew to false
repeat until OKNew
set NewItem to display dialog "Enter new value :" default answer ""
set OKNew to (button returned of NewItem is "OK") and (text returned of NewItem is not "")
set theNumber to text returned of NewItem
set BaseList to baseList & {theNumber} -- to add the new item in the BaseList
end repeat
else
set theNumber to text 1 thru 13 of (item 1 of chosen) -- as chosen returns a list by default it must be flattened
end if

I also experienced some issues with the signature and the attachement together. if you remove the line "make new attachment..." the signature line is working. Also if you move the signature line before the attachement line, and make a break before the attachement, the signature will be OK. bug ?
Then, based on my tests, if you do not want signature at all, just delete the line "set message signature..." by default, no signature will be set.
My last comment is to reduce your script by adding the content directly in property list of the "make new outgoing message..."
tell application "Mail"
set newMessage to make new outgoing message with properties {visible:true, subject:TheNumber, content:TheContent}
tell newMessage
make new to recipient with properties {address:faxboxmail}
if onRun > 1 then
make new attachment with properties {file name:theFile as alias} at after last paragraph
end if
end tell
activate
end tell
I tried it, and it is creates mail with no content and no signature as expected (Mail 8.2)

Related

Is it possible to filter a list using boolean test specifier?

I'm interested: is it possible to filter a list using boolean test specifier?
Like this for example:
set allList to every message of mailbox "Sent Messages" of iCloud account "iCloud"
-- gives an execution error:
set filtList2 to every item of allList whose subject contains "test"
while this line works as expected:
set filtList1 to every message of mailbox "Sent Messages" of iCloud account "iCloud" whose subject contains "test"
I understand, that I can filter the list using a loop:
set filtList3 to {}
repeat with aMessage in allList
if subject of aMessage contains "test" then
set end of filtList3 to aMessage
end if
end repeat
end tell
Why boolean test specifier gives me an error on a list?
Thank you!
OK, it turns out, that boolean test specifiers for lists are not implemented in AppleScript, so one should use loops instead.

AppleScript read data every few minutes

I run a screen which display some data on the menu bar
the variables are taken from "~/Desktop/_MyData.plist"
everything work fine, however when the data change on _MyData.plist
How, can I make the script getting the new data ? I guess we can't expect AppleScript to detect file change and then run the script but is there a way to get the plist data on idle maybe and keeping running the whole script.
Here is the part that only getting the data :
property theAccountNumberFromPlist : ""
property SNNumber : ""
property appName : ""
property devicesID : ""
property domainEMAIL : ""
property fullEmail : ""
property purchaseDate : ""
property thename : ""
property theList : ""
set the plistfile_path to "~/Desktop/_MyData.plist.plist"
tell application "System Events"
set p_list to property list file (plistfile_path)
-- read the plist data
set theAccountNumberFromPlist to value of property list item "AccountNumber" of p_list as text
set SNNumber to value of property list item "SNNUMBER" of p_list as text
set appName to value of property list item "appName" of p_list as text
set devicesID to value of property list item "devicesID" of p_list as text
set domainEMAIL to value of property list item "domainEMAIL" of p_list as text
set fullEmail to value of property list item "fullEmail" of p_list as text
set purchaseDate to value of property list item "purchaseDate" of p_list as text
set thename to value of property list item "thename" of p_list as text
end tell
Here is the whole script :
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
property StatusItem : missing value
property selectedMenu : ""
property theDisplay : ""
property defaults : class "NSUserDefaults"
property internalMenuItem : class "NSMenuItem"
property externalMenuItem : class "NSMenuItem"
property newMenu : class "NSMenu"
property theAccountNumberFromPlist : ""
property SNNumber : ""
property appName : ""
property devicesID : ""
property domainEMAIL : ""
property fullEmail : ""
property purchaseDate : ""
property thename : ""
property theList : ""
set the plistfile_path to "~/Desktop/_MyData.plist.plist"
tell application "System Events"
set p_list to property list file (plistfile_path)
-- read the plist data
set theAccountNumberFromPlist to value of property list item "AccountNumber" of p_list as text
set SNNumber to value of property list item "SNNUMBER" of p_list as text
set appName to value of property list item "appName" of p_list as text
set devicesID to value of property list item "devicesID" of p_list as text
set domainEMAIL to value of property list item "domainEMAIL" of p_list as text
set fullEmail to value of property list item "fullEmail" of p_list as text
set purchaseDate to value of property list item "purchaseDate" of p_list as text
set thename to value of property list item "thename" of p_list as text
end tell
if not (current application's NSThread's isMainThread()) as boolean then
display alert "This script must be run from the main thread." buttons {"Cancel"} as critical
error number -128
end if
on menuNeedsUpdate:(menu)
my makeMenus()
end menuNeedsUpdate:
on makeMenus()
newMenu's removeAllItems()
repeat with i from 1 to number of items in someListInstances
set this_item to item i of someListInstances
set thisMenuItem to (current application's NSMenuItem's alloc()'s initWithTitle:this_item action:"someAction:" keyEquivalent:"")
(newMenu's addItem:thisMenuItem)
(thisMenuItem's setTarget:me) -- required for enabling the menu item
if i is equal to 3 then
(newMenu's addItem:(current application's NSMenuItem's separatorItem)) -- add a seperator
end if
end repeat
end makeMenus
on someAction:sender
--MenuItem
end someAction:
-- create an NSStatusBar
on makeStatusBar()
set bar to current application's NSStatusBar's systemStatusBar
set StatusItem to bar's statusItemWithLength:-1.0
-- set up the initial NSStatusBars title
StatusItem's setTitle:(theAccountNumberFromPlist & " " & thename & " " & fullEmail & " " & SNNumber & " " & appName)
-- set up the initial NSMenu of the statusbar
set newMenu to current application's NSMenu's alloc()'s initWithTitle:"Custom"
newMenu's setDelegate:me (*
*)
StatusItem's setMenu:newMenu
end makeStatusBar
my makeStatusBar()
Make use of an idle handler.
on idle
-- do your stuff
return 5 -- idle handler will be executed again in 5 seconds.
end
Therefore you need to save your script as application. Here's the essential part from apples docs:
idle and quit Handlers for Stay-Open Applications
By default, a script application that receives a run or open command
handles that single command and then quits. In contrast, a stay-open
script application (one saved as Stay Open in Script Editor) stays
open after it is launched.
A stay-open script application can be useful for several reasons:
Stay-open script applications can receive and handle other commands in
addition to run and open. This allows you to use a script application
as a script server that, when it is running, provides a collection of
handlers that can be invoked by any other script.
Stay-open script applications can perform periodic actions, even in
the background, as long as the script application is running.
Two particular handlers that stay-open script applications often
provide are an idle handler and a quit handler.
idle Handlers
If a stay-open script application includes an idle handler,
AppleScript sends the script application periodic idle commands—by
default, every 30 seconds—allowing it to perform background tasks when
it is not performing other actions.
If an idle handler returns a positive number, that number becomes the
rate (in seconds) at which the handler is called. If the handler
returns a non-numeric value, the rate is not changed. You can return 0
to maintain the default delay of 30 seconds.
For example, when saved as a stay-open application, the following
script beeps every 5 seconds:
on idle
    beep
    return 5
end idle
The result returned from a handler is just the result of the last statement, even if it doesn’t include the word return explicitly.

Choose from list and write to plist as string, not as array

I've got the following which displays two text input prompts and then queries for PPTP VPN configs and asks the user to 'choose from list'. The results are then saved to a plist file however vpn_name is always encoded inside of two blank arrays. I've tried setting the datatype of vpn_name to string but the same applies.
How can I get this to encode simply as:
<dict>
<key>password</key>
<string>fnkdslfsd</string>
<key>username</key>
<string>fnsdkfnds</string>
<key>vpn_name</key>
<string>SAMSUNG_Android</string>
</dict>
as opposed to how it's currently encoding:
<dict>
<key>password</key>
<string>fnkdslfsd</string>
<key>username</key>
<string>fnsdkfnds</string>
<key>vpn_name</key>
<array>
<array>
<string>SAMSUNG_Android</string>
</array>
</array>
</dict>
Applescript:
property key_path : "~/Library/Preferences/vpn-keys.plist"
property user_name : ""
property pass_word : ""
property vpn_name : ""
tell application "Finder"
if user_name is "" then
set dialog_1 to display dialog "Please enter your server username: " default answer ""
set the user_name to the text returned of dialog_1
end if
if pass_word is "" then
set dialog_2 to display dialog "Please enter your server password: " default answer ""
set the pass_word to the text returned of dialog_2
end if
if vpn_name is "" then
tell application "System Events"
tell current location of network preferences
set VPN_list to get name of every service whose (kind is greater than 11 and kind is less than 17)
end tell
end tell
vpn_name as string
set vpn_name to {choose from list VPN_list with prompt "Select Office VPN"}
end if
tell application "System Events"
set the parent_dictionary to make new property list item with properties {kind:record}
set the plistfile_path to key_path
set this_plistfile to ¬
make new property list file with properties {contents:parent_dictionary, name:plistfile_path}
make new property list item at end of property list items of contents of this_plistfile ¬
with properties {kind:string, name:"username", value:user_name}
make new property list item at end of property list items of contents of this_plistfile ¬
with properties {kind:string, name:"password", value:pass_word}
make new property list item at end of property list items of contents of this_plistfile ¬
with properties {kind:string, name:"vpn_name", value:vpn_name}
end tell
end tell
choose from list returns a list of items selected and returns false when canceled. You defined an empty string before choose from list command is invoked but in AppleScript that doesn't have any effect between lines. So what you can do is test if the results are false before continuing because unlike a display dialog the script continues when the user pressed the cancel button. Then set value of new property list item to item 1 of vpn_name.
property key_path : "~/Library/Preferences/vpn-keys.plist"
property user_name : ""
property pass_word : ""
property vpn_name : ""
tell application "Finder"
if user_name is "" then
set dialog_1 to display dialog "Please enter your server username: " default answer ""
set the user_name to the text returned of dialog_1
end if
if pass_word is "" then
set dialog_2 to display dialog "Please enter your server password: " default answer ""
set the pass_word to the text returned of dialog_2
end if
if vpn_name is "" then
set vpn_name to false
tell application "System Events"
tell current location of network preferences
set VPN_list to get name of every service whose (kind is greater than 11 and kind is less than 17)
end tell
end tell
set vpn_name to choose from list VPN_list with prompt "Select Office VPN"
if vpn_name is false then return --stop, user pressed cancel
end if
tell application "System Events"
set the parent_dictionary to make new property list item with properties {kind:record}
set the plistfile_path to key_path
set this_plistfile to ¬
make new property list file with properties {contents:parent_dictionary, name:plistfile_path}
make new property list item at end of property list items of contents of this_plistfile ¬
with properties {kind:string, name:"username", value:user_name}
make new property list item at end of property list items of contents of this_plistfile ¬
with properties {kind:string, name:"password", value:pass_word}
make new property list item at end of property list items of contents of this_plistfile ¬
with properties {kind:string, name:"vpn_name", value:item 1 of vpn_name}
end tell
end tell

How to create an event e-mail using AppleScript and a text file?

I have to create an Outlook e-mail using a text file as a template. I have seen some script but nothing like what I need. I also need to edit some contents of a file before creating the e-mail. I also need to know what would be the best place to put that template file.
There is a script for creating a new email but I dont know how to load test and edit from here.
Updated Code----
--read source from the file
set theFile to "/Users/eclit/Desktop/MeetingTemplate.html"
open for access theFile
set fileContents to (read theFile)
close access theFile
tell application "Microsoft Outlook"
set newMessage to make new outgoing message with properties {subject:"Hooray for automation", content:fileContents & return & return}
make new recipient at newMessage with properties {email address:{name:"Jim Shank", address:"jim.shank#example.com"}}
open newMessage
end tell
Please help.
--read source from the file
set theFile to "/Users/eclit/Desktop/MeetingTemplate.html"
open for access theFile
set fileContents to (read theFile)
close access theFile
on replaceText(find, replace, subject)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set subject to text items of subject
set text item delimiters of AppleScript to replace
set subject to "" & subject
set text item delimiters of AppleScript to prevTIDs
return subject
end replaceText
-- log fileContents
on categoryForName(_categoryName)
tell application "Microsoft Outlook"
try
return category _categoryName
on error
try
-- Getting by name doesn't always work.
repeat with _category in categories
if _category's name is _categoryName then return _category
end repeat
end try
make new category with properties {name:_categoryName}
end try
return category _categoryName
end tell
end categoryForName
log fileContents
tell application "Microsoft Outlook"
--set newMessage to make new outgoing message with properties {subject:"Hooray for automation", content:fileContents & return & return}
set theCategory to my categoryForName("Work")
--set theCategory to make new category with properties {name:"Fanciful", color:{12345, 23456, 11111}}
set newEvent to make new calendar event with properties {subject:"Dial In : +442034333797 Conference code: 5270687926", content:fileContents}
--make new recipient at newMessage with properties {email address:{name:"Jim Shank", address:"jim.shank#example.com"}}
--open newMessage
open newEvent
end tell

How to retrieve email body of outgoing mail in apple mail using applescript?

How can i retrieve email body, recipient email address, subject etc of outgoing mail.
thanks for any help.
EDIT:-
I have added one custom button in outgoing mail toolbar see image.
And the functionality of that button is send mail and save all mail detail on my server also so how can i get that detail like,
both email addresses "To"="test#gmail.com" and "cc"="test2#gmail.com", subject="My subject" , and email body="My email body"
How can i achieve this ?
Through this apple script you can retrieve mail content so please try it.
tell application "System Events"
tell process "Mail"
set _Contents to {}
set _contentDetail to {}
set the _contentDetail to entire contents of UI element 1 of scroll area 3 of window 1
repeat with _value in _contentDetail
set str to ""
if class of _value is static text then
set str to value of _value
end if
set str to str & return
set _Contents to _Contents & str
end repeat
return get _Contents as string
end tell
end tell

Resources