How to set text in Pages with applescript - applescript

Is there any way to set text from applescript in macos 12.4? I get an
Error: Can’t set every text of \"The\" to \"hello\".-10006""
with the script below, when I run having a test document up in pages.
tell application "Pages"
set mydoc to first document
try
set t to "hello"
set b to body text of mydoc
set p to first paragraph of b
set w to first word of p
set text of w to t
on error errMsg number errorNumber
display dialog "Error: " & errMsg & errorNumber as text
end try
end tell
Thanks!

Related

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

Applescript file not found

I am using this syntax in my Applescript but I am getting an error file not found. I am sure the file is there. Is there anything wrong with this?
set rtfFile to "Macintosh HD:Users:ash:Documents:Core:_Marketing:xlsafe_p.rtf" as alias
Here is a little handler to check at which point of your path the error occurs:
set rtfFile to "Macintosh HD:Users:ash:Documents:Core:_Marketing:xlsafe_p.rtf"
checkPath(rtfFile)
on checkPath(aPathToCheck)
set oldATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set pathElements to text items of aPathToCheck
set AppleScript's text item delimiters to oldATID
set d to ""
set tempPath to ""
repeat with i from 1 to (count pathElements)
set tempPath to tempPath & d & item i of pathElements
try
set foo to tempPath as alias
on error
display alert "The path " & return & tempPath & return & "is not valid!" buttons {"OK"}
return false
end try
set d to ":"
end repeat
display alert "The path " & return & tempPath & return & "is valid!" buttons {"Yeah!"}
return true
end checkPath
Here it says The path Macintosh HD:Users:ash is not valid! because I'm logged in with other credentials than you ;-) Please try this handler and tell us the result!
Enjoy, Michael / Hamburg
Try to choose the file with this, copy the text from the alert, and compare the result.
display alert ((choose file) as text)

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

Folder image to iTunes artwork in applescript : not working anymore

Anyone of you guys could point me to why this applescript (which was working on snow leopard) does not work anymore on 10.7 Lion.
It intend to copy the file named "folder.jpg" from each album folder into ID3 tags of selected mp3 in iTunes.
property tempfile : ((path to temporary items as string) & "itunespicturefile_temporaire.pict")
global vPictFolder
tell application "iTunes"
set v_TrackSelection to selection
if v_TrackSelection is not {} then
repeat with vTrack in v_TrackSelection
set vPictFolder to my (ParentFromPath(location of vTrack as string)) as text
set thisPict to my getpictData("folder.jpg")
if thisPict is not "" then
delete artworks of vTrack
set data of artwork 1 of vTrack to (thisPict)
end if
end repeat
else
display dialog ("select tracks first !")
end if
end tell
on getpictData(vAlbumpictName)
tell application "Finder" to tell file vAlbumpictName of folder vPictFolder to if exists then
set t_file to it as string
else
display dialog vPictFolder & vAlbumpictName
return ""
end if
do shell script "/opt/local/bin/convert " & quoted form of POSIX path of t_file & " " & quoted form of POSIX path of tempfile
display dialog "ok"
return read (tempfile as alias) from 513 as picture
end getpictData
on ParentFromPath(thePath)
set wantPath to true
set thePath to (thePath as text)
set saveDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set pathAsList to text items of thePath
if the last character of thePath is ":" then
set idx to (the number of text items in thePath) - 2
else
set idx to -2
end if
if wantPath then
set folderName to ((text items 1 through idx of pathAsList) as text) & ":"
else
set folderName to item idx of pathAsList
end if
set AppleScript's text item delimiters to saveDelim
return folderName
end ParentFromPath
Because Picture format (".pict") is not supported on Lion and newer OS.
You can use the term picture to read a image file (JPEG, PNG, ...), the format will not be in the Picture format , but the original format of the file.
tell application "iTunes"
set v_TrackSelection to selection
if v_TrackSelection is not {} then
repeat with vTrack in v_TrackSelection
set vPictFolder to my (ParentFromPath(location of vTrack as string)) as text
set thisPict to my getpictData(vPictFolder & "folder.jpg")
if thisPict is not "" then
delete artworks of vTrack
set data of artwork 1 of vTrack to (thisPict)
end if
end repeat
else
display dialog ("select tracks first !")
end if
end tell
on getpictData(tFile)
try
return (read (tFile as alias) as picture)
end try
display dialog tFile
return "" -- not exists
end getpictData

Use applescript to write iCal data to file

I am trying to get applescript to read todays ical events and write them to a file. The code is as follows:
set out to ""
tell application "iCal"
set todaysDate to current date
set time of todaysDate to 0
set tomorrowsDate to todaysDate + (1 * days)
repeat with c in (every calendar)
set theEvents to (every event of c whose start date ≥ todaysDate and start date < tomorrowsDate)
repeat with current_event in theEvents
set out to out & summary of current_event & "
"
end repeat
end repeat
end tell
set the_file to (((path to documents folder) as string) & "DesktopImage:ical.txt")
try
open for access the_file with write permission
set eof of the_file to 0
write out to the_file
close access the_file
on error
try
close access the_file
end try
end try
return out
The ical items are getting returned correctly and the file ical.txt is created correctly in the folder Documents/DesktopImage, but the file is blank.
Even if I substitute
write out to the_file
with
write "Test string" to the_file
it still comes up blank
any ideas?
Ty
To fix something, you first need to find out what's wrong. You can get the error message when your error handler is invoked, then display it:
on error msg
display dialog "Error accessing " & (the_file as string) & ": " & msg
Once you do this, you quickly see the problem: the_file is a string, not a file, so all the file operations will fail. Define the_file as:
set the_file to alias (((path to documents folder) as string) & "DesktopImage:ical.txt")

Resources