sent pictures by email applescript - email

I can get my script to sent a pictures by email, its sending the text of the files instead the real files (Hello, here is the screenshot of iLog :~:ilogscreenshot.png)
Plus for sone raison , when I take the first screenshot , nothing happen until I change the windows, and then I have the dialog "are you happy with the screenshot"
Any idea where I should change the code?
Many thanks
-- Start of screenshot
display dialog "You will have to select the screenshot area you want when you see the cross cursor" buttons {"OK"} default button 1
-- Select the aera :
tell application "iLog" to activate
delay 1
do shell script "screencapture -i ~/ilogscreenshot.png"
--" & winID
display dialog "Are you happy with the selected aera ?" buttons {"Yes", "No"} default button 2
if the button returned of the result is "Yes" then
-- action for Yes
display dialog "Ok we gonna no sent this by email " buttons {"OK"} default button 1
else
-- action for NO
display dialog "No worry, we gonna try again then" buttons {"OK"} default button 1
tell application "iLog" to activate
delay 1
do shell script "screencapture -i ~/ilogscreenshot.png"
--" & winID
end if
-- end of screenshot
-- sceenshot saved files
set theAttachment1 to POSIX file "~/ilogscreenshot.png"
-- end of screenshot saved files
display dialog "Sent to which email" default answer "#apple.com" buttons {"OK"} default button 1
set recipientAddress to text returned of result
set theSubject to "iLog Sceenshot!"
(* This will past the clopboard on the content
*)
set theContent to "Hello, here is the screenshot of iLog " & theAttachment1
tell application "Mail"
##Create the message
set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
##Set a recipient
tell theMessage
make new to recipient with properties {address:recipientAddress}
##Send the Message
end tell
end tell

I can't test it with iLog, but the screenshot part works like this:
set screenShot to ((path to desktop) as text) & "ilogscreenshot.png"
# it's easier to check the Screenshot on the Desktop
set happyUser to "No"
repeat until happyUser is "Yes"
tell application "iLog" to activate
do shell script "screencapture -i " & quoted form of POSIX path of screenShot
display dialog "Are you happy with the selected aera ?" buttons {"Yes", "No"} default button 2
set happyUser to button returned of the result
end repeat
Adding the screenshot to an Outgoing Message (here is a good guide):
set theSubject to "iLog Sceenshot!"
set theContent to "Hello, here is the screenshot of iLog:" & return & return
set recipientAddress to "test#test.de"
tell application "Mail"
set theMessage to make new outgoing message with properties {visible:true, subject:theSubject, content:theContent}
tell theMessage to make new to recipient with properties {address:recipientAddress}
tell content of theMessage to make new attachment with properties {file name:file screenShot} at after last paragraph
--send theMessage
end tell

Related

Catching punctuation in AppleScript for use in a Mail Rule

I get a ton of spam mail where the from or reply to has punctuation in the address. Instead of creating a giant rule with a bunch of rows with Contains "!" and such, I want to use a script in a mail rule to send everything from an address with punctuation other than a dot and an at to the trash.
What can I replace = "hello world" in the example below to catch punctuation?
tell application "Mail"
set theSelection to selection
set theMessage to item 1 of theSelection
subject of theMessage
if subject of theMessage = "hello world" then
set mailbox of theMessage to mailbox "Trash"
end if
end tell
I've looked at some get shell script and sed examples but didn't understand how to specify that I only want to find punctuation.
What you’re looking for is a regular expression; AppleScript doesn’t have them by default. There are a couple of ways around this.
The easiest AppleScript way is to have a whitelist of valid characters, and then check your string to see if it contains anything not in that whitelist.
Because we don’t want to be trashing messages willy-nilly while testing, I’ve replaced the trash line with a display dialog line. Because the AppleScript calls a function, I also do this outside of the tell block. While you can call local functions inside a tell block, it’s easier in a short script like this to not worry about it.
property goodCharacters : "abcdefghijklmnopqrstuvwxyz.#"
tell application "Mail"
set theSelection to selection
set theMessage to item 1 of theSelection
set theSubject to theSubject of theMessage
end tell
if badCharacter(theSubject) is true then
display dialog theSubject & " is bad."
--tell application "Mail" to set the mailbox of theMessage to mailbox "Trash"
end if
--return true of the text contains any non-good character
on badCharacter(theText)
repeat with theCharacter in theText
if goodCharacters does not contain theCharacter then
return true
end if
end repeat
return false
end badCharacter
The badCharacter handler returns true if any of the characters in the given string are not in the list of good characters. Otherwise, it returns false.
You may also find these useful when you switch from testing against the subject to testing against what you said you want, which is the from and the reply-to:
set theSender to extract address from the sender of theMessage
set theReply to extract address from the reply to of theMessage
If you’re familiar with JavaScript, you may find JXA (JavaScript for Automation) more useful, because JavaScript does have regular expressions. In the upper left of Script Editor, change “AppleScript” to “JavaScript”.
mail = Application('Mail');
trash = mail.mailboxes.byName("Trash")
theSelection = mail.selection();
theMessage = theSelection[0];
theSender = theMessage.sender().replace(/^.*<([^>]+)>/, "$1");
theReply = theMessage.replyTo();
theSubject = theMessage.subject();
//text.search returns the index of the match
//it returns -1 on no match
//so if the result is 0 or greater, it found a match
//the caret (^) reverses the sense of the brackets
//where [a-z.#] would match *on* any character from a to z as well as the period and #
//[^a-z.#] matches any any character *other than* a-z, ., and #
if (theSubject.search(/[^a-z.#]/i) >= 0) {
mail.move(theMessage, {to: trash})
}
Other options might include using a third-party tool to add regular expressions to AppleScript. You might also use do shell script to use sed or some other command-line tool to parse the text you need parsed; I’m inclined to think running unknown text to the command line is dangerous, however.
And AppleScript does have tools for strings besides =. As shown above, it has contains (or does not contain), but it also has starts with and ends with. You can even get the words of any string, which will remove all punctuation.

AppleScript Error Code -1700

This is the error that pops up when I try my script out:
Script Error ---- Can’t make {button returned:"Enter", text returned:"testing"} into type Unicode text.
I'm trying to have users enter a Username so that they that have their Documents folder be linked to a file server.
I have that part separate but now on this part of having the user input their username so when I have ln -s /Volumes/Drive/Documents Documents the username will automatically pull from the script and input it by itself so the Documents folder and File server could be linked.
Basically trying to make it to where the user doesn't have to go into the terminal and link the two together. I'm not sure why the error code is popping up other than it is probably having to deal with the set Username part of the script so not completely sure.
set Username to (display dialog "Enter your NetID Username" default answer "" buttons {"Cancel", "Enter"} default button 2)
tell application "Terminal"
activate
do script "'ln -s /Volumes/Drive/Documents Documents'" & Username
end tell
return input
The error message
Can’t make {button returned:"Enter", text returned:"testing"} into type Unicode text.
is pretty clear.
display dialog returns a record, not a string so you have to write
set Username to text returned of (display dialog "Enter your NetID Username" default answer "" buttons {"Cancel", "Enter"} default button 2)
Or if you need to handle the button and the returned text write
set {button returned:buttonReturned, text returned:textReturned} to display dialog "Enter your NetID Username" default answer "" buttons {"Cancel", "Enter"} default button 2

How to programmatically zip files exactly like Finder? [duplicate]

I noticed that the command line 'zip' tool and Mac OS X's 'Compress XXX' option (available via right click in finder) are giving different output files. Not only is the size of the file a few hundred bytes bigger but the content is significantly different as well.
How can I find out what command the Finder is using for compression?
The answer is in man ditto:
The command:
ditto -c -k --sequesterRsrc --keepParent src_directory archive.zip
will create a PKZip archive similarly to the Finder's Compress function-
ality.
Take a look at An AppleScript to compress a Finder selection article.
try
tell application "Finder"
set theSelection to the selection
set selectionCount to count of theSelection
if selectionCount is greater than 1 then
error "Please select only one Finder item before running this script."
else if selectionCount is less than 1 then
error "Please select one Finder item before running this script."
else
set theItem to (item 1 of theSelection) as alias
set destFolder to (container of theItem) as alias
set itemName to name of theItem
end if
end tell
do shell script ("ditto -c -k --sequesterRsrc --keepParent " & quoted form of POSIX path of theItem & space & quoted form of (POSIX path of destFolder & itemName & ".zip"))
on error theError
tell me
activate
display dialog "Error: " & theError buttons {"OK"} default button 1 with icon stop
end tell
end try

Need a fix for an Applescript bug in my effort to determine existence of renamed file

I have a script to copy all email attachments in a selected set of emails to a folder after some renaming. Because the attachments sometimes have identical names at origin, even with renaming, I need to add something like " copy" to subsequent versions so they don't save atop one another. With some programming knowledge but very little understanding of AppleScript, I cobbled this together:
tell application "Mail"
set theMessages to selection
set theOutputFolder to (choose folder) as string
repeat with a from 1 to length of theMessages
set theMessage to item a of theMessages
set {year:y, month:m, day:d} to date sent of theMessage
set theDate to (y * 10000 + m * 100 + d) as string
set theAttachments to every mail attachment of theMessage
repeat with b from 1 to length of theAttachments
set theAttachment to item b of theAttachments
set theAttachmentName to theDate & " " & name of theAttachment
set theSavePath to theOutputFolder & theAttachmentName
tell application "System Events" to exists file theSavePath
repeat while the result
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"."}
set delimitedList to every text item of theSavePath
set suffix to "." & last item of delimitedList
try
copy text items 1 thru -2 of theSavePath to theSavePathBase
on error
copy theSavePath to theSavePathBase
end try
-- display dialog "theSavePath pre- : " & theSavePath
-- display dialog "theSavePathBase & ' copy' & suffix pre- " & theSavePathBase & " copy" & suffix
set AppleScript's text item delimiters to oldDelims
copy theSavePathBase & " copy" & suffix to theSavePath
-- display dialog "theSavePath post- : " & theSavePath
tell application "System Events" to exists file theSavePath -- <<< *** BOMBS HERE
-- display dialog "Made it past existence check."
end repeat
try
display dialog "preparing to save..."
save theAttachment in theSavePath
on error errText number errNum
display dialog errText
end try
end repeat
end repeat
end tell
It works, except that when the need to fix the name of a copy is encountered, it bombs at the indicated location with this message: "System Events got an error: Can’t make {"...Desktop:Mail Attachments:20140830 Resumé 2014", "pages", " copy", ".zip"} into type integer." Something about the way I reconstituted the name changed the type to something the exists checker can't handle.
Any help would be appreciated. Help with an explanation would be better, since I'm not an AppleScripter but am interested. Thanks!
Your error message is telling you the problem. Notice the brackets {} around the error message.
Can’t make {"...Desktop:Mail Attachments:20140830 Resumé 2014", "pages", " copy", ".zip"} into type integer."
That's indicating that this is a list of items, not a string. As such you need to make it a string first therefore change...
copy text items 1 thru -2 of theSavePath to theSavePathBase
to:
copy (text items 1 thru -2 of theSavePath) as text to theSavePathBase
With that being said, I don't think your actual copy command (as follows) will work. It seems you're wanting the copy command to rename the file and copy it all in one step.
copy theSavePathBase & " copy" & suffix to theSavePath
The copy command won't be able to find "...Desktop:Mail Attachments:20140830 Resumé 2014.pages. copy.zip" because it doesn't exist. That's the name you want for the renamed file. I think you're best approach would be to use the "cp" unix executable command to preform your copy because it can copy and rename in one step. Something like this although you'll have to figure out how to get the actual value for the variable "theAttachmentCurrentPath".
do shell script "cp " & quoted form of POSIX path of theAttachmentCurrentPath & space & quoted form of POSIX path of (theOutputFolder & text 1 thru -5 of theAttachmentName & " copy" & text -4 thru -1 of theAttachmentName)

Copy and Paste text from Dialogue Box in Applescript

i'm writing a script that needs to copy and paste text from an entry in a dialogue box
set query to text returned of (display dialog "Enter Text" default answer "" buttons {"Input", "Cancel"} default button 1)
query
tell application "System Events"
keystroke "c" using command down
keystroke "v" using command down
end tell
when i run this script it copies and pastes "set query to text..."
how can I copy the query that I entered ? i'm going to paste the text somewhere else later but I need to figure out how to actually copy the text. I was using keystroke query but that was taking a long time when the strings were long
Use set the clipboard:
display dialog "" default answer ""
set the clipboard to text returned of result
If others get here searching for how to for example copy a long error message from a dialog shown by OS X, you can use Accessibility Inspector, which is in /Applications/Xcode.app/Contents/Applications/.