Applescript to replace message sent and received in Messages application - applescript

I found this script to change uppercase and lowercase of characters in messages but i want to modify this code to replace some string in text, for ex: message received is "i want to eat", this message will be change to "i like to eat" or ":)" change to ":-)".
property lowercaseCharacters : "abcdefghijklmnopqrstuvwxyz"
property uppercaseCharacters : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
on intercaps(str)
set theCharacters to characters of str
set theCount to 1
repeat with aChar in theCharacters
if (aChar is in uppercaseCharacters or aChar is in lowercaseCharacters) then
if (theCount mod 2) is equal to 1 then
set contents of aChar to character (offset of aChar in lowercaseCharacters) of uppercaseCharacters
else
set contents of aChar to character (offset of aChar in uppercaseCharacters) of lowercaseCharacters
end if
end if
set theCount to theCount + 1
end repeat
return theCharacters as string
end intercaps
using terms from application "Messages"
on message sent theMessage for theChat
return intercaps(theMessage)
end message sent
on message received theMessage from theBuddy for theChat
return intercaps(theMessage)
end message received
on chat room message received theMessage from theBuddy for theChat
return intercaps(theMessage)
end chat room message received
end using terms from

Here is one method:
set myText to replace_chars("i want to eat", "want", "would like")
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

Related

how do i fix this error ,error "The variable bufferdName is not defined." number -2753 from "bufferdName"?

i need help with my applescript program
set UnixPath to POSIX path of ((path to me as text) & "::")
set term to get id of application "Terminal"
on notify(t)
display notification t
end notify
notify("starting " & term)
to replaceText(someText, oldItem, newItem)
(*
replace all occurances of oldItem with newItem
parameters - someText [text]: the text containing the item(s) to change
oldItem [text, list of text]: the item to be replaced
newItem [text]: the item to replace with
returns [text]: the text with the item(s) replaced
*)
set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, oldItem}
try
set {itemList, AppleScript's text item delimiters} to {text items of someText, newItem}
set {someText, AppleScript's text item delimiters} to {itemList as text, tempTID}
on error errorMessage number errorNumber -- oops
set AppleScript's text item delimiters to tempTID
error errorMessage number errorNumber -- pass it on
end try
return someText
end replaceText
on getMyName()
set myPath to path to me as text
if myPath ends with ":" then
set n to -2
else
set n to -1
end if
set AppleScript's text item delimiters to ":"
set myName to text item n of myPath
if (myName contains ".") then
set AppleScript's text item delimiters to "."
set myName to text 1 thru text item -2 of myName
end if
set AppleScript's text item delimiters to ""
return myName
end getMyName
set nameToFix to getMyName()
set bufferedName to replaceText(nameToFix, " ", "\\ ")
set lua to UnixPath & bufferdName & ".app/Contents/Resources/" & "lua"
set programSet to UnixPath & bufferdName & ".app/Contents/Resources/Scripts/lua_program.lua"
set mainComand to "program_app_temp/lua_program.lua"
set do to "dofile(" & quoted form of mainComand & ")"
tell application "Terminal"
activate
tell application "System Events"
keystroke "mkdir ~/ai_app_temp"
keystroke return
keystroke "cp " & ai & " ~/ai_app_temp/"
keystroke return
keystroke lua
keystroke return
keystroke do
delay 1
keystroke return
end tell
end tell
when i run my program it hits an error and says
error "The variable bufferdName is not defined." number -2753 from "bufferdName" . how do i fix that? i am new to applescript
It looks like a simple spelling mistake. Look carefully at the spelling in these lines of code
set bufferedName to replaceText(nameToFix, " ", "\\ ")
set lua to UnixPath & bufferdName & ".app/Contents/Resources/" & "lua"
set programSet to UnixPath & bufferdName & ".app/Contents/Resources/Scripts/lua_program.lua"
bufferedName is the variable you set, but you are using bufferdName <-- spelled differently

Applescript convert variable into a string with commas for CSV

I have a variable that contains {"THIS", "THAT"} that I am trying to write to a csv so that the csv formats as THIS,THAT. Current it just spits out as THISTHAT.
I think I need to repeat though the variable but I am not sure...
the code is as follows (check the ---> for the important bits):
tell application "Adobe InDesign CC 2014"
delete unused swatches of document 1
set _NotUSED to {"None", "Paper", "Black", "Registration", "Keyline", "ImageLabel", "C=0 M=0 Y=0 K=37", "C=0 M=100 Y=100 K=0", "Map this to white ->", "Dieline", "C=0 M=100 Y=0 K=0"} as string
try
---> Get the variables
set _UpDatedList to get (name of swatches of document 1 whose name is not in _NotUSED)
on error
display alert {"Your document has no spot colours"}
end try
end tell
set filePath to (path to desktop as text) & "Pantones.csv"
---> Set the theString to the variables
set theString to _UpDatedList as string
set theResult to writeTo(filePath, theString, text, false)
if not theResult then display dialog "There was an error writing the data!"
on writeTo(targetFile, theData)
try
set openFile to open for access file targetFile with write permission
---> write the variables to csv
write theData to openFile
close access openFile
return true
on error
try
close access file targetFile
end try
return false
end try
end writeTo
Try this, the easiest way to convert a list to CSV is to use text item delimiters.
The main problem is the coercion to string in the 3rd line. Delete as string.
tell application "Adobe InDesign CC 2014"
delete unused swatches of document 1
set _NotUSED to {"None", "Paper", "Black", "Registration", "Keyline", "ImageLabel", "C=0 M=0 Y=0 K=37", "C=0 M=100 Y=100 K=0", "Map this to white ->", "Dieline", "C=0 M=100 Y=0 K=0"}
try
set _UpDatedList to (get name of swatches of document 1 whose name is not in _NotUSED)
on error
display alert "Your document has no spot colours" buttons {"Cancel"}
return -- abort the script
end try
end tell
set filePath to (path to desktop as text) & "Pantones.csv"
set {TID, text item delimiters} to {text item delimiters, ","}
set csvString to _UpDatedList as text
set text item delimiters to TID
set theResult to writeTo(filePath, csvString)
if not theResult then display dialog "There was an error writing the data!"
on writeTo(targetFile, theData)
try
set openFile to open for access file targetFile with write permission
write theData to openFile
close access openFile
return true
on error
try
close access file targetFile
end try
return false
end try
end writeTo
Here is another option. Just modify your WriteTo handler as shown below to tell it to add a comma after every item in the list, except the last item.
-- Define theString and the target file
set theString to {"This", "That"}
set theFile to (path to desktop as text) & "Pantones.csv"
-- Execute the write handler
set theResult to writeTo (theFile, theString)
on writeTo(targetFile, theData)
set openFile to open for access file targetFile with write permission
set theCount to the count of items in theData
set n to 0
--write to the target file & add a comma after every item except the last
repeat theCount times
set n to n + 1
if n is not theCount then write item n of theData & "," to openFile as string
if n is theCount then write item n of theData to openFile as string
end repeat
close access openFile
end writeTo
In this example, the end result will be your file "Pantones.csv" with the following text:
This,That

"Expected “,” but found property error using Apple Script

I using Apple script to get the required details from Microsoft outlook. Its totally working fine on the Mountian Lion OsX(10.8.3) but when i use the same script its failing and throwing me the error ""Expected “,” but found property".
Below is the Apple Script which i am using.
on encodeXML(s)
set AppleScript's text item delimiters to "&"
set components to every text item of s
set AppleScript's text item delimiters to "&"
set s to components as string
set AppleScript's text item delimiters to ""
set AppleScript's text item delimiters to "<"
set components to every text item of s
set AppleScript's text item delimiters to "<"
set s to components as string
set AppleScript's text item delimiters to ">"
set components to every text item of s
set AppleScript's text item delimiters to ">"
set s to components as string
set AppleScript's text item delimiters to "\""
set components to every text item of s
set AppleScript's text item delimiters to """
set s to components as string
set AppleScript's text item delimiters to "'"
set components to every text item of s
set AppleScript's text item delimiters to "&apos;"
set s to components as string
set AppleScript's text item delimiters to ""
return s
end encodeXML
on meetingNumber(s)
set AppleScript's text item delimiters to "Meeting Number:"
set components to every text item of s
if (count of components) is less than 2 then
return ""
end if
set s to second text item of components
set AppleScript's text item delimiters to "To join"
set components to every text item of s
set s to first text item of components
set AppleScript's text item delimiters to "<"
set components to every text item of s
set s to first text item of components
set AppleScript's text item delimiters to "&"
set components to every text item of s
set s to first text item of components
set AppleScript's text item delimiters to "----"
set components to every text item of s
set s to first text item of components
set AppleScript's text item delimiters to " "
set components to every text item of s
set AppleScript's text item delimiters to ""
return components as string
end meetingNumber
on trimContents(s)
set AppleScript's text item delimiters to "Meeting Number:"
set components to every text item of s
if (count of components) is less than 2 then
return ""
end if
set s to second text item of components
set AppleScript's text item delimiters to "To join"
set components to every text item of s
set s to first text item of components
set AppleScript's text item delimiters to "<"
set components to every text item of s
set s to first text item of components
set AppleScript's text item delimiters to "&"
set components to every text item of s
set s to first text item of components
set AppleScript's text item delimiters to "----"
set components to every text item of s
set AppleScript's text item delimiters to ""
return "Meeting Number: " & first text item of components
end trimContents
on getEventKeys(theEvent)
set r to ""
tell application "Microsoft Outlook"
set r to r & " <wxp:meeting>
"
set r to (r & " <wxp:appID>" & id of theEvent as string) & "</wxp:appID>
"
set t to (content of theEvent as string)
set t to my trimContents(t)
set r to (r & " <wxp:content>" & my encodeXML(t)) & "</wxp:content>
"
set r to r & " </wxp:meeting>
"
end tell
return r
end getEventKeys
on getEvent(theEvent)
set retVal to ""
tell application "Microsoft Outlook"
set retVal to retVal & "<wxp:meeting>"
set retVal to (retVal & "<wxp:subject>" & my encodeXML(subject of theEvent as string)) & "</wxp:subject>"
set retVal to (retVal & "<wxp:organizer>" & organizer of theEvent as string) & "</wxp:organizer>"
set retVal to (retVal & "<wxp:startDate>" & start time of theEvent as string) & "</wxp:startDate>"
set retVal to (retVal & "<wxp:endDate>" & end time of theEvent as string) & "</wxp:endDate>"
set retVal to (retVal & "<wxp:appID>" & ID of theEvent as string) & "</wxp:appID> "
set outType to ""
if (is recurring of theEvent) then
set recur to recurrence of theEvent
set recurType to recurrence type of recur as string
if recurType is "daily" then
set outType to "DAILY"
else if recurType is "weekly" then
set outType to "WEEKLY"
else if recurType is "absolute monthly" or recurType is "relative monthly" then
set outType to "MONTHLY"
else if recurType is "absolute yearly" or recurType is "relative yearly" then
set outType to "YEARLY"
end if
set retVal to retVal & "<wxp:repeatType>" & outType & "</wxp:repeatType>
"
set retVal to retVal & "<wxp:interval>" & occurrence interval of recur & "</wxp:interval>
"
if end type of end date of recur as string is "end numbered type" then
set retVal to retVal & "<wxp:afterMeetingNumber>" & data of end date of recur & "</wxp:afterMeetingNumber>
"
else if end type of end date of recur as string is "end date type" then
set retVal to (retVal & "<wxp:expirationDate>" & data of end date of recur as string) & "</wxp:expirationDate>
"
end if
if recurType is "relative monthly" then
set retVal to retVal & "<wxp:weekInMonth>" & ordinal of recur & "</wxp:weekInMonth>"
end if
if recurType is "weekly" or recurType is "relative monthly" then
set bitmap to 0
if sunday of days of week of recur then
set bitmap to bitmap + 1
end if
if monday of days of week of recur then
set bitmap to bitmap + 2
end if
if tuesday of days of week of recur then
set bitmap to bitmap + 4
end if
if wednesday of days of week of recur then
set bitmap to bitmap + 8
end if
if thursday of days of week of recur then
set bitmap to bitmap + 16
end if
if friday of days of week of recur then
set bitmap to bitmap + 32
end if
if saturday of days of week of recur then
set bitmap to bitmap + 64
end if
set retVal to retVal & "<wxp:dayInWeek>" & bitmap & "</wxp:dayInWeek>
"
end if
else
set retVal to retVal & "<wxp:repeatType></wxp:repeatType>
"
end if
if (has reminder of theEvent) then
set retVal to (retVal & " <wxp:reminder>" & reminder time of theEvent as string) & "</wxp:reminder>
"
end if
--set attendees to attendee of theEvent
set retVal to retVal & " <wxp:attendees>
"
repeat with theAttendee in required attendee of theEvent
set retVal to retVal & my writeAttendee(theAttendee, "REQUIRED")
end repeat
repeat with theAttendee in optional attendee of theEvent
set retVal to retVal & my writeAttendee(theAttendee, "OPTIONAL")
end repeat
repeat with theAttendee in resource attendee of theEvent
set retVal to retVal & my writeAttendee(theAttendee, "RESOURCE")
end repeat
set retVal to retVal & " </wxp:attendees>
"
set retVal to (retVal & " <wxp:content>" & my encodeXML(content of theEvent as string)) & "</wxp:content>
"
set retVal to retVal & " </wxp:meeting>
"
end tell
return retVal
end getEvent
on writeAttendee(theAttendee, theType)
tell application "Microsoft Outlook"
set retVal to ""
if (status of theAttendee as string) is not "declined" then
set em to email address of theAttendee
set retVal to retVal & " <wxp:attendee>
"
try
set retVal to (retVal & " <wxp:name>" & my encodeXML(name of em as string)) & "</wxp:name>
"
end try
set retVal to (retVal & " <wxp:ID>" & address of em as string) & "</wxp:ID>
"
set retVal to retVal & " <wxp:type>" & theType & "</wxp:type>
"
set retVal to retVal & " </wxp:attendee>
"
end if
return retVal
end tell
end writeAttendee
Can some on please look into it and let me know what i am doing wrong here.
Thanks In Advance.
Ravi Kishore.
The line set em to email address of theAttendee lies outside the tell application "Microsoft Outlook" block and so AppleScript does not understand the term email address. Move this statement inside a tell application "Microsoft Outlook" block and you should be okay.

Automatically add incoming call to the conference using Skype API on Mac

I am trying to write an AppleScript which will start the Skype and call who are online and add incoming calls automatically after the conference is started. I am stuck at the last part of this implementation (starred in the code below). The code that is in the repeat loop.
Can you please help me implement a way to add the incoming calls to the existing conference call without putting the call on hold?
Thanks in advance!
My system is Mac OS X Lion (10.7.5)
PS. I have got a lot of help from other people's works so far. That's how I could do this much.
tell application "System Events"
set powerCheck to ((application processes whose (name is equal to "Skype")) count)
if powerCheck = 0 then
my launch_skype()
else if powerCheck = 1 then
return
end if
end tell
## FUNCTIONS ##
on dismiss_skype_api_security()
tell application "System Events" to tell process "Skype"
set window_name to "Skype API Security"
set ok_text to "OK"
set radio_text to "Allow this application to use Skype"
tell application "System Events" to tell process "Skype"
if window window_name exists then
click radio button radio_text of radio group 1 of window window_name
delay 2
click button ok_text of window window_name
end if
end tell
delay 1
end tell
end dismiss_skype_api_security
on launch_skype()
tell application "Skype"
set statusList to {"RINGING", "ROUTING", "UNPLACED"}
delay 2
try
set status to "COMMAND_PENDING"
repeat until status is not equal to "COMMAND_PENDING"
set status to send command "GET USERSTATUS" script name "auto call"
if status is equal to "COMMAND_PENDING" then
my dismiss_skype_api_security()
end if
end repeat
#### CALL ONLINE CONTACTS + AUTO ACCEPT ####
activate
delay 1
set OnlineContacts to my get_online_contacts()
delay 1
send command "CALL " & OnlineContacts script name "auto call"
delay 2
-- Set first call ID as master
set firstCall to send command "SEARCH ACTIVECALLS" script name "auto call"
set firstCallID to last word of firstCall -- What if the last guy leaves the call ?!?!?
set firstStatus to send command "GET CALL " & firstCallID & " STATUS" script name "auto call"
if statusList contains the last word of firstStatus then -- First Call
set MasterCallID to firstCallID
end if
**set callID to ""
repeat until callID is "CALLS"
delay 3
set status to send command "GET CALL " & firstCallID & " STATUS" script name "auto call"
if the last word of status is "RINGING" then --Someone is calling to join the call
set calls to send command "SEARCH ACTIVECALLS" script name "auto call"
set callID to last word of calls
--send command "ALTER CALL " & callID & " JOIN_CONFERENCE " & mainCallID script name --"auto call"
end if
end repeat**
on error number -2753
quit
end try
end tell
end launch_skype
### GET ONLINE CONTACTS ###
on get_online_contacts()
global OnlineFriends
set OnlineFriends to {}
tell application "Skype"
set SkypeStatus to send command "GET CONNSTATUS" script name "auto call"
if SkypeStatus is "CONNSTATUS ONLINE" then
set AppleScript's text item delimiters to " "
set Friends to send command "SEARCH FRIENDS" script name "auto call"
set Friends to my ReplaceString(Friends, " ", ",")
set Friends to my ReplaceString(Friends, ",,", ",")
set FriendsList to my SplitList(Friends, ",")
set NumFriends to number of items in FriendsList
repeat with i in FriendsList
if (i begins with "DISABLEDxmpp:") or (i begins with "USERS") or (i begins with "ugur") or (i is "echo123") or (i begins with "esr") or (i begins with "ayt") or (i begins with "Zaf") then
else
set FriendStatus to send command "GET USER " & i & " ONLINESTATUS" script name "auto call"
if text item 4 of FriendStatus is "ONLINE" then
set aUser to i
set aUser to my JoinList(aUser, " ")
set end of OnlineFriends to aUser
end if
end if
end repeat
if (count OnlineFriends) > 0 then
set OnlineFriends to my JoinList(OnlineFriends, ", ")
else
set beginning of OnlineFriends to "No Skype Friends online at this time."
end if
else
set beginning of OnlineFriends to "Skype is offline."
end if
end tell
return OnlineFriends
end get_online_contacts
on JoinList(l, del)
set RetVal to ""
set OldDel to AppleScript's text item delimiters
set AppleScript's text item delimiters to del
set RetVal to l as string
set AppleScript's text item delimiters to OldDel
return RetVal
end JoinList
on SplitList(t, del)
set RetVal to {}
set OldDel to AppleScript's text item delimiters
set AppleScript's text item delimiters to del
set RetVal to every text item of t
set AppleScript's text item delimiters to OldDel
return RetVal
end SplitList
on ReplaceString(theText, oldString, newString)
set OldDel to AppleScript's text item delimiters
set AppleScript's text item delimiters to oldString
set tempList to every text item of theText
set AppleScript's text item delimiters to newString
set theText to the tempList as string
set AppleScript's text item delimiters to OldDel
return theText
end ReplaceString

Applescript Question: Repeat an action in MAIL on selected messages

Question: I would like to run an applescript against a list of selected messages in Apple OS X Mail.
Right now, the script I wrote will work on a single selection only, so I am guessing the issue is how to loop it for all items selected. Any help would be appreciated.
Here is the script:
tell application "Mail"
set theSelection to selection
set theSelectedMessage to item 1 of theSelection
set theSelectedMessageSender to sender of theSelectedMessage
set theSelectedMessageRecipient to address of to recipients of theSelectedMessage
set theSelectedMessageSenderName to extract name from sender of theSelectedMessage
set theSelectedMessageSenderAddress to extract address from sender of theSelectedMessage
set theSelectedMessageSubject to subject of theSelectedMessage
set theSelectedMessageContent to content of theSelectedMessage
set MessageText to ¬
"This email (" & theSelectedMessageRecipient & ") does NOT ¬
care to receive emails regarding this matter." & return & return & ¬
"This email was originally delivered to: " & ¬
theSelectedMessageRecipient & return & return & ¬
"Remove this email from your list: " & ¬
theSelectedMessageRecipient & ¬
return & return & ¬
"---------- ORIGINAL MESSAGE ----------------"
set theMessage to make new outgoing message with properties {visible:true, subject:"REMOVE: RE:" & theSelectedMessageSubject, content:MessageText & theSelectedMessageContent, reply to:theSelectedMessageRecipient}
tell theMessage
make new to recipient at end of to recipients with properties {name:theSelectedMessageSenderName, address:theSelectedMessageSenderAddress}
end tell
end tell
set theSelectedMessage to item 1 of theSelection
Replace this with:
repeat with theSelectedMessage in theSelection
Just before the last line, add:
end repeat

Resources