Change contents of new message in outlook - macos

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

Related

macOS Photos and AppleScript: Quicker organization method

The Goal:
In macOS Photos App, highlight/select a bulk of photos, iterate each one.
AppleScript will tell Photos to quickview the photo, ask what 'Album(s)' to add the photo to, then it will ask for a title and caption.
I'm doing this because the 'Info' panel that provides this function doesn't allow for rapid entry, and it's pretty small to work with.
So I suppose I need a couple things:
Show the picture (tell Photos to quick look it)
Identify the 'My Album' folders (eliminate the smart folders from the mix)
Prompt and Update the Title and the Caption (called name and description in the def)
Add the photo to the selected Albums.
I'm not sure if it's just me, but the AppleScript scripting on Photos is just not something I can wrap my head around very well.
Here's what I have so far, but I was hoping you can help.
tell application "Photos"
--Find All Albums
set thefullList to the name of every album of every folder
-- Need:Figure out how to strip Smart Albums out of this query
-- Set comma as the delim to separate the folders/albums
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set thefullList to every item of thefullList
set AppleScript's text item delimiters to oldDelimiters
-- Test the output (fails to delim, and won't display the string)
display dialog thefullList
-- Clean list of Albums
set albumNames to {thefullList}
-- Get the selected photos from Photos
set theSelection to (get selection)
-- Iterate through each photo
repeat with i in theSelection
-- Tell Photos to 'quick look'
-- Need:(no idea how to make that work yet)
-- Ask the user to choose the Album(s) this photo should go into
set theAlbumChoice to choose from list albumNames with prompt "Where should this photo go?"
-- Tell the Album that this photo is now added
set theAlbum to theAlbumChoice
add i to theAlbum
-- Ask for Title and Caption
display dialog "What's the Title of this Photo?"
set theTitle to text returned of result
display dialog "What's the Caption of this Photo?"
set theCaption to text returned of result
-- Get the Photo ID for adding MetaData
set selectionID to id of item i of theSelection
-- Set the Title and caption
set name of media item id selectionID to theTitle
set description of media item id selectionID to theCaption
end repeat
end tell
property albumNames : {}
property albumIDs : {}
tell application "Photos"
activate
my getAlbumNames(it) -- get all album names and IDs (recursively)
repeat with aPhoto in (get selection) -- process each selected photo
try -- Get the Photo ID
set selectionID to id of aPhoto
on error errorMessage
set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "\""
set selectionID to text item 2 of errorMessage
set AppleScript's text item delimiters to ATID
end try
spotlight media item id selectionID -- Tell Photos to 'quick look'
-- Ask the user to choose the Album(s) this photo should go into
set theAlbumChoice to choose from list albumNames with prompt "Where should this photo go?"
if theAlbumChoice is false then return
-- Find destination folder's ID and add photo to it
repeat with k from 1 to count albumNames
if (item 1 of theAlbumChoice) is (item k of albumNames) then
set theAlbumID to item k of albumIDs
exit repeat
end if
end repeat
add {media item id selectionID} to album id theAlbumID
-- Ask for Title and Caption, and set them
display dialog "What's the Title of this Photo?" default answer ""
set theTitle to text returned of result
display dialog "What's the Caption of this Photo?" default answer ""
set theCaption to text returned of result
set name of media item id selectionID to theTitle
set description of media item id selectionID to theCaption
end repeat
end tell
on getAlbumNames(aFolder) -- recursive handler
tell application "Photos"
set albumNames to albumNames & name of albums of aFolder
set albumIDs to albumIDs & id of albums of aFolder
repeat with subFolder in (get folders of aFolder)
my getAlbumNames(subFolder)
end repeat
end tell
end getAlbumNames

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?

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

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

applescript to open google search query in youtube

I am using the code below to open youtube with google query in applescript as the new google does not carry my search string to youtube.Unable to open url in firefix with the google search string in firefox. I really appreciate any help.Thanks in advance.
I am getting an error here :
tell application "Firefox" to activate
tell application "System Events"
keystroke "l" using command down
keystroke "c" using command down
set tr to get the clipboard
end tell
return trimgoogle(tr)
on trimgoogle(sourceAddress)
set AppleScript's text item delimiters to {"#"}
set addressParts to (every text item in sourceAddress) as list
set AppleScript's text item delimiters to ""
set nameOnly to item 2 of addressParts
set AppleScript's text item delimiters to {"&"}
set addressParts2 to (every text item in nameOnly) as list
set AppleScript's text item delimiters to ""
set nameOnly1 to item 1 of addressParts2
set nameOnly2 to nameOnly1
set AppleScript's text item delimiters to {"="}
set addressParts21 to (every text item in nameOnly2) as list
set AppleScript's text item delimiters to ""
set nameOnly12 to item 2 of addressParts21
set newurl to "http://www.youtube.com/results?search_query=" & nameOnly12
return newurl
end trimgoogle
tell application "Firefox"
OpenURL newurl
end tell
Try this:
tell application "Firefox"
open location newurl
end tell
EDIT:
Firefox does not activate itself automatically, so I just tried this:
tell application "Firefox"
activate
open location "http://www.wikipedia.com/"
end tell
which works here.
Here more about encoding text:
http://www.macosxautomation.com/applescript/sbrt/sbrt-08.html
EDIT2:
OK, I see what you mean, that would be solved like this:
tell application "Firefox" to activate
tell application "System Events"
keystroke "l" using command down
keystroke "c" using command down
set tr to get the clipboard
end tell
set newurl to my trimgoogle(tr)
tell application "Firefox"
activate
open location newurl
end tell
on trimgoogle(sourceAddress)
set AppleScript's text item delimiters to {"#"}
set addressParts to (every text item in sourceAddress) as list
set AppleScript's text item delimiters to ""
set nameOnly to item 2 of addressParts
set AppleScript's text item delimiters to {"&"}
set addressParts2 to (every text item in nameOnly) as list
set AppleScript's text item delimiters to ""
set nameOnly1 to item 1 of addressParts2
set nameOnly2 to nameOnly1
set AppleScript's text item delimiters to {"="}
set addressParts21 to (every text item in nameOnly2) as list
set AppleScript's text item delimiters to ""
set nameOnly12 to item 2 of addressParts21
set newurl to "http://www.youtube.com/results?search_query=" & nameOnly12
return newurl
end trimgoogle

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