How to Send the Output of a Dialog Box to an email adress in applescript - macos

I would like to be able to send the output of a dialog box to my email. What would be the best way to do such a thing?
It would be something sort of like this:
repeat
display dialog "Enter some text:" buttons {"Git Goin"} default answer ""
set theNewInfo to text returned of result
if theNewInfo ≠ "" then exit repeat
end repeat
Its a really simple script for a proof of concept, but what I want is as follows: When they enter any text into the dialog box, for that text to be sent to my email, regardless of what it contains.The Subject would say "NewInfo" and the body would contain the text entered into the dialog box

You should post what code you have... too many questions still to be able to answer you reliably. What are you wanting to send to the email message? email addy? subject? body? etc.
Basically, you capture the result of the dialog and then put it into a "mailto:" URL string and then use 'open location' on the URL. This should be enough to get you started:
set dialogResult to display dialog "Enter the subject" default answer "My subject" buttons {"me#example.com", "you#example.com"} default button 1
set theAddress to button returned of dialogResult
set theSubject to text returned of dialogResult
set theBody to "This%20is%20the%20body%20text:%0AMore%20text"
-- Must encode entities, spaces as %20 and line breaks as %0A (%0D%0A), etc.
set theSubject to findReplace(" ", "%20", theSubject)
set theSubject to findReplace(return, "%0A", theSubject)
set theURL to "mailto:" & theAddress & "?subject=" & theSubject & "&body=" & theBody
open location theURL
on findReplace(f, r, s)
set otid to AppleScript's text item delimiters
set AppleScript's text item delimiters to f
set s to text items of s
set AppleScript's text item delimiters to r
set s to s as string
set AppleScript's text item delimiters to otid
return s
end findReplace

Related

Apple Script put clipboard list in quotes

Does anyone know of an applescript that will take whatever is on the clipboard and wrap it in quotes. (Ideally triggered by key shortcut, but i can set that up myself)
Example (Copied Data from excel col)
Beer
Whisky
Chronic
Would turn into
'Beer','Whisky','Chronic'
I can't find this anywhere and I can only do some basic stuff in Applescript but I totally think it would help a lot of devs/db admins!
Assuming the source is plain text and the items are separated by newline characters
you can do it with text item delimiters
set theText to "Beer
Whisky
Chronic"
set {TID, text item delimiters} to {text item delimiters, {linefeed, return}}
set theList to text items of theText
set text item delimiters to "','"
set theResult to "'" & (theList as text) & "'"
set text item delimiters to TID
theResult
To get the data directly from the clipboard replace the first line with
set theText to the clipboard
thanks to #vadian for the help there - the final working script
takes input from clipboard, transforms and then puts back to clipboard
set theText to (do shell script "pbpaste")
set {TID, text item delimiters} to {text item delimiters, {linefeed, return}}
set theList to text items of theText
set text item delimiters to "','"
set theResult to "'" & (theList as text) & "'"
set text item delimiters to TID
set the clipboard to theResult
theResult
I've set it up as a service using automator and assigned a keyboard shortcut for quick transform.

Replace All Error, Applescript

I am trying to get this script to click "replace all" using keystrokes.
I am getting an error:
"System Events got an error: Can’t get button \"Replace All\"." number
-1728 from button "Replace All"
tell application "Mail"
set theSenderList to {}
set theMessages to the selected messages of message viewer 0
repeat with aMessage in theMessages
set end of theSenderList to {address of to recipient of aMessage, " OR"}
end repeat
set AppleScript's text item delimiters to " "
set the clipboard to (theSenderList as string)
set AppleScript's text item delimiters to " "
beep
end tell
set clip to (the clipboard as text)
tell application "Numbers" to tell document 1 to tell sheet 1 to tell table 1
set value of cell "a9" to clip
end tell
tell application "Numbers" to tell document 1 to tell sheet 1 to tell table 1
set value of cell "b9" to current date
end tell
tell application "Numbers"
activate
end tell
tell application "System Events"
delay 1.0
keystroke "f" using command down
keystroke "#gmail.com"
keystroke tab
keystroke ""
delay 2
click button "Replace All"
end tell
It appears that you are trying to replace the occurrence of "#gmail.com" in a bunch of email addresses with blanks, thus deleting that portion of the address. You could do that much more easily in plain AppleScript:
set targetAddresses to {"fred#gmail.com", "wilma#gmail.com", "barney#gmail.com"}
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "#gmail.com"
set fixedAddresses to {}
repeat with eachAddress in targetAddresses
set end of fixedAddresses to text item 1 of eachAddress
end repeat
set AppleScript's text item delimiters to astid
fixedAddresses
-->{"fred", "wilma", "barney"}
Is this close to what you are trying to achieve?

I want to set variable once in an applescript droplet but it makes me set it for every file

I wrote an applescript droplet where I would like to:
drag one or more images onto the droplet
3 display dialogs appear asking 'width', 'height', 'format'
process all dropped images using the above text returned of the
3 display dialogs
Currently, the 3 display dialogs appear for each image (e.g. 3 images = 9 dialogs appear). Is there a way I can only answer these dialogs once? Here's my script:
on run
display dialog "This is a droplet"
end run
on open draggedItems
set tid to AppleScript's text item delimiters
--ask for new width
set newWidth to text returned of (display dialog "New Width" default answer ¬
"45" buttons {"Continue…", "Cancel"} ¬
default button 1)
--ask for new height
set newHeight to text returned of (display dialog "New Height" default answer ¬
"45" buttons {"Continue…", "Cancel"} ¬
default button 1)
--ask for formatType
set newFormat to text returned of (display dialog "Image Format" default answer ¬
"jpg" buttons {"Continue…", "Cancel"} ¬
default button 1)
--repeat
repeat with i in draggedItems
set theFile to (i as alias)
set theFilePath to (the POSIX path of theFile)
set fullFileName to name of (info for theFile without size)
set AppleScript's text item delimiters to "."
set fileNameNoExtension to first text item of fullFileName
--set fileExtension to second text item of fullFileName
set AppleScript's text item delimiters to tid
do shell script ("/usr/local/bin/convert " & quoted form of theFilePath & " -resize " & newWidth & "x" & newHeight & "\\! ~/desktop/" & fileNameNoExtension & "." & newFormat)
end repeat
end open
on open draggedItems is already an repeat loop, if you drop 4 files on this droplet
on open draggedItems
display dialog (draggedItems as text)
end open
you will get 4 different dialogs.
You could do something like this
property askingForNew : true
on open draggedItems
if askingForNew is true then
--display dialogs
set askingForNew to false
end if
display dialog (draggedItems as text)
end open
or drag the folder on the droplet and go through the files of it (like this).

Change contents of new message in outlook

I wan to change the content of the currently composed message and add the string *WF* at the end. Below is the script, I get an error Microsoft Outlook got an error: Can’t set content of draft window id 9490 to ...
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
tell application "Microsoft Outlook"
activate
set theWindow to window 1
-- check it's really draft
if class of theWindow is not draft window then
display dialog "Not a draft"
return
end if
-- save the draft
save theWindow
--get the id of the object of the draft window
set myObjectID to id of (object of theWindow)
-- to make it secure
set myContent to content of message id myObjectID
--edit recipient
-- set theBCC to bcc recipient of message id myObjectID
-- set emailString to theBCC
make new bcc recipient at message id myObjectID with properties {email address:{name:"Ittay Dror", address:"idror#akamai.com"}}
set myContent to my replace_chars(myContent, "</body>", "*WF*</body>")
set the content of theWindow to myContent
end tell
you can't set the content of a window. Please set the content of the message:
set the content of message id myObjectID to myContent
Cheers,
Michael / Hamburg

Issue with applescript function

I'm trying to the function replaceChars in the below script but getting the following error:
error "Finder got an error: Can’t continue replace_chars." number -1708
The intention is to add the script to finder as a button so that I can simply click it to copy the path to my clipboard. I'm adding file://localhost/ so that the link can then be used when shared with users by email as a direct link to a folder on the local network. If possible I would also like to add to clipboard the same for Windows machines.
If you could offer any guidance as to the task above it would be much appreciated, this is my first attempt at programming with applescript so I'm not that knowledgable of how things are done.
Heres the code:
on appIsRunning(appName)
tell application "System Events"
set isRunning to ((application processes whose (name is equal to appName)) count)
end tell
if isRunning is greater than 0 then
return true
else
return false
end if
end appIsRunning
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
if appIsRunning("Finder") then
tell application "Finder"
set thePath to "file://localhost/" as text
set theTarget to (target of front Finder window) as text
set the clipboard to thePath & replace_chars(theTarget, ":", "/") as text
end tell
end if
AppleScript is looking for a replace_chars handler in the Finder's scripting dictionary. You can either make it into my replace_chars to have AS look in the script, or (probably better) move the set the clipboard to thePath & replace_chars(theTarget, ":", "/") as text line out of the tell block altogether.
You can make it into a one-line script:
tell application "Finder" to set the clipboard to "file://localhost" & (target of front Finder window as text)'s POSIX path
Replace your replace_chars(theTarget, ":", "/") call with replace_chars(theTarget, ":", "/") of me:
set the clipboard to thePath & replace_chars(theTarget, ":", "/") of me as text

Resources