Is this a type mismatch - applescript

I'm new to AppleScript and having trouble understanding something. Why doesn't the function valid_hex return true for these items? Clearly the data items are being obtained correctly from the tsv by reading paragraphs and text item 1 and 2 because the output string looks fine.
Is there a type mismatch that doesn't let valid_hex() do its job?
set inputStr to "8-1 Black\t232323\r\n8-2 Brown\tB5674D\r\n8-3 Orange\tFF7538\r\n8-4 Yellow\tFCE883\r\n8-5 Green\t1CAC78\r\n8-6 Blue\t1F75FE\r\n8-7 Violet (Purple)\t926EAE\r\n8-8 Red\tEE204D"
set accepted to {}
set rejected to {}
set acceptedCount to 0
set rejectedCount to 0
set atids to AppleScript's text item delimiters
set AppleScript's text item delimiters to tab
repeat with p in (paragraphs of inputStr)
set aLine to text of (p as string)
repeat 1 times
set colorName to text item 1 of aLine
set hexColor to text item 2 of aLine
log hexColor & " named " & colorName & " is valid: " & valid_hex(hexColor)
end repeat
end repeat
on valid_hex(s)
set validhex to {"#", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "a", "b", "c", "d", "e", "f"}
if not (length of s = 6 or (length of s = 7 and s begins with "#")) then return false
repeat with c in (text items of s)
if validhex contains c then
set status to true
else
set status to false
exit repeat
end if
end repeat
return status
end valid_hex
UPDATE: Based on the accepted answer the original problem has been resolved. This is the full script. It takes a delimited list of hex colors with names and creates an Xcode .colorset folder for each one that can be dragged directly into an Xcode Assets catalog to be used as a named color.
It works pretty well, but if an error file is created, it doesn't go into the same working_folder.
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
try
set src to (choose file with prompt "choose input file")
set o to (open for access src)
set inputStr to (read o)
close access o
end try
tell application "Finder"
set working_path to container of (src) as string
end tell
set accepted to {}
set rejected to {}
set acceptedCount to 0
set rejectedCount to 0
repeat with aLine in (get paragraphs of inputStr)
set old_delimits to AppleScript's text item delimiters -- Save the original delimiters
set AppleScript's text item delimiters to tab
set {colorName, hexColor} to text items of aLine
set AppleScript's text item delimiters to old_delimits -- Restore the original delimiters
repeat 1 times
if not valid_hex_color(hexColor) then
set rejectedCount to rejectedCount + 1
copy "Rejected " & "\"" & colorName & "\"" & " with hex value: " & hexColor & "\n" to the end of rejected
exit repeat
else
set acceptedCount to acceptedCount + 1
set redComponent to text 1 thru 2 of hexColor
set greenComponent to text 3 thru 4 of hexColor
set blueComponent to text 5 thru 6 of hexColor
set jsonString to "{\n\t\"info\": {\n\t\t\"version\": 1,\n\t\t\"author\": \"xcode\"\n\t},\n\t\"colors\": [\n\t\t{\n\t\t\t\"idiom\": \"universal\",\n\t\t\t\"color\": {\n\t\t\t\t\"color-space\": \"srgb\",\n\t\t\t\t\"components\": {\n\t\t\t\t\t\"red\": \"0x" & redComponent & "\",\n\t\t\t\t\t\"green\": \"0x" & greenComponent & "\",\n\t\t\t\t\t\"blue\": \"0x" & blueComponent & "\",\n\t\t\t\t\t\"alpha\": \"1.000\"\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t]\n}"
tell application "Finder"
set folderName to colorName & ".colorset"
set fldr to (make new folder at working_path with properties {name:folderName})
end tell
set resultFilePath to (working_path as string) & folderName & ":Contents.json"
set outFile to (open for access resultFilePath with write permission)
write jsonString to outFile starting at 0
close access outFile
copy colorName & "\n" to the end of accepted
end if
end repeat
end repeat
if rejectedCount > 0 then
set summary to "\nrejected " & rejectedCount & ":\n--------------------\n" & rejected & "\naccepted " & acceptedCount & ":\n--------------------\n" & accepted
set errorFilePath to (working_path as string) & ":RejectedItems.txt"
log working_path & errorFilePath
set errorFile to (open for access errorFilePath with write permission)
write summary to errorFile starting at 0
close access errorFile
display dialog ("Rejected " & rejectedCount & " items, see " & errorFilePath & ".")
end if
on valid_hex_color(s)
set validhex to "0123456789ABCDEF"
if s begins with "#" then set s to text 2 thru -1 of s
if the length of s ≠ 6 then return false
repeat with c in characters of s
if validhex does not contain c then return false
end repeat
true
end valid_hex_color

The problem is in the line
repeat with c in (text items of s)
At this moment text item delimiters is set to tab so there is only one text item which is always the entire string.
To get each character replace it with
repeat with c in (get characters of s)
The get keyword is important to retrieve the list only once.
The first repeat loop is a bit cumbersome, this is sufficient
repeat with aLine in (get paragraphs of inputStr)
set {colorName, hexColor} to text items of aLine
log hexColor & " named " & colorName & " is valid: " & valid_hex(hexColor)
end repeat
And don't forget to reset text item delimiters
set AppleScript's text item delimiters to atids
A more sophisticated way to check the string is with AppleScriptObjC and Regular Expression (put the use lines at the beginning of the script)
use AppleScript version "2.5"
use framework "Foundation"
on valid_hex(s)
set regex to current application's NSRegularExpression's regularExpressionWithPattern:"^#?[0-9A-Fa-f]{6}$" options:0 |error|:(missing value)
return (regex's numberOfMatchesInString:s options:0 range:{location:0, |length|:(count s)}) as integer is 1
end valid_hex

Related

Export Apple photos library for importing it elsewhere

I intented to export all my photos from Mac to any other environment and this suggestion finally provided the base to solve this with applescript. That way the photos are always scaled (getting bigger than the original) but at least it works.
I know it's long but as I struggled that long with this problem, so I post the whole working solution below as an answer. Maybe someone has a better suggestion. I have refused to reverse engineer the contents in sqlite tables Photos is using, as this just may change with the next version.
Problems are various
There is no guarantee how Photos walks through the albums and folders. And as it sometimes just stops working, you have to guarantee some order, or you will never finish. So I introduced a way to start at a particular album at a particular photo. The order within the album seems to be stable. The log output written shows the unique ids of album and photo (as names may not be unique) to be able to restart it at this point. Most of the time the second attempt just works.
I did not find a way to store photos in order, so I created one single directory for each photo and stored each photo in a separate directory, so I can reimport them in order. Problem is that Photos always uses the original file name and just counts upwards if the name exists already. Using different cameras just makes it worse.
All attributes I wrote into a text file per photo, so I can reimport them later.
Error handling is quite tricky. The photos library sometimes just requests the user to press a button, which makes it hard to script it.
That's unfortunately the best I was able to come up with, it is my 1st applescript. This worked on an old Mac with Sierra and it works on more recent versions with Catalina as well. I'm almost sure the newest version will not complain. I'm still searching for a better solution.
There is little support to run it from inside Apple's script editor, only command line provides all options.
#!/usr/bin/osascript
global startAlbum, startPhoto, match, dummy, match, photoCnt, infoFd
global errorIndicator
on writePhotoAndData(thePath, mediaItem, mediaAlbum)
set ind to "X"
using terms from application "Photos"
set fName to filename of mediaItem
set fId to id of mediaItem
set photoCnt to photoCnt + 1
-- export each media to separate directory -> only chance to keep the order
if dummy then
if infoFd ≠ missing value then
set s to id of mediaAlbum & tab & id of mediaItem & tab & thePath & linefeed as text
write s to infoFd as «class utf8»
end if
else
makeEmptyPosixPath(thePath)
set exportPath to POSIX file thePath
set infoFile to POSIX file (thePath & "/" & "info.txt")
set infoText to "id" & tab & id of mediaItem & linefeed & "file" & tab & filename of mediaItem & linefeed & "album" & tab & name of mediaAlbum & linefeed
if exists name of mediaItem then set infoText to infoText & "name" & tab & name of mediaItem & linefeed
if exists description of mediaItem then set infoText to infoText & "desc" & tab & description of mediaItem & linefeed
if exists date of mediaItem then
set d to date of mediaItem
set infoText to infoText & "date" & tab & short date string of d & space & time string of d & linefeed
end if
if exists altitude of mediaItem then set infoText to infoText & "alt" & tab & altitude of mediaItem & linefeed
if exists location of mediaItem then set infoText to infoText & "location" & tab & location of mediaItem & linefeed
if exists keywords of mediaItem then
tell mediaItem to set myKeywords to keywords
repeat with keyword in myKeywords
set infoText to infoText & "keyword" & tab & keyword & linefeed
end repeat
end if
set fd to open for access infoFile with write permission
set eof fd to 0 -- of fd?
write infoText to fd starting at eof as «class utf8»
close access fd
try
tell mediaAlbum
--Not sure whether this does anything, so removed
--set settings to "JPEG - Original Size"
export {mediaItem} to (exportPath as alias)
end tell
set errorIndicator to 0
on error errStr number errNum
if errNum = -1712 then --timeout
set ind to "E"
set errorIndicator to errorIndicator + 1
if errorIndicator >= 3 then
error "3 errors in a row - exiting"
end if
else
error errStr number errNum
end if
end try
end if
log ind & tab & photoCnt & tab & id of mediaAlbum & tab & id of mediaItem & tab & name of mediaAlbum & tab & filename of mediaItem
end using terms from
end writePhotoAndData
on walkAlbum(theAlbum, thePath)
if match = 0 then
if id of theAlbum = startAlbum then
set match to 1
if startPhoto is missing value then
set match to 2
end if
else
return
end if
else if match = 1 then
if id of theAlbum is not equal to startAlbum then
set match to 3
end if
end if
set photoNum to 0
using terms from application "Photos"
set albumPath to thePath & name of theAlbum & "/"
repeat with mediaItem in media items of theAlbum
set photoNum to photoNum + 1
if match = 1 then
if id of mediaItem = startPhoto then
set match to 2
end if
else if match = 2 then
set match to 3 --photo after the photo chosen
end if
if match = 3 then
writePhotoAndData(albumPath & photoNum, mediaItem, theAlbum)
end if
end repeat
end using terms from
end walkAlbum
on walkFolder(theFolder, thePath)
using terms from application "Photos"
repeat with containedFolder in folders of theFolder
walkFolder(containedFolder, thePath & name of containedFolder & "/")
end repeat
repeat with containedAlbum in albums of theFolder
walkAlbum(containedAlbum, thePath)
end repeat
end using terms from
end walkFolder
on makePosixPath(tPath)
do shell script "mkdir -p " & quoted form of tPath
end makePosixPath
on makeEmptyPosixPath(tPath)
do shell script "rm -rf " & quoted form of tPath & " && mkdir -p " & quoted form of tPath
end makeEmptyPosixPath
on makeFolder(tPath)
do shell script "mkdir -p " & quoted form of POSIX path of tPath
end makeFolder
on walkFile(fileName, fileOffset as integer)
set thisOffset to 0
set saveDelim to text item delimiters of AppleScript
set walkFd to open for access POSIX file fileName
set rawLine to read walkFd before linefeed as «class utf8»
repeat
set thisOffset to thisOffset + 1
if fileOffset = 0 or thisOffset ≥ fileOffset then
set thisLine to rawLine as text
set text item delimiters of AppleScript to tab
set splitLine to text items of thisLine
set text item delimiters of AppleScript to saveDelim
set albumIdString to item 1 of splitLine
set photoIdString to item 2 of splitLine
set pathString to item 3 of splitLine
tell application "Photos"
set thisAlbum to album id albumIdString
set thisMedia to media item id photoIdString
end tell
writePhotoAndData(pathString, thisMedia, thisAlbum)
log "O" & tab & thisOffset
end if
try
set rawLine to read walkFd before linefeed as «class utf8»
on error errTxt number errNum
if errNum = -39 then --end of file
exit repeat
else
error "Error reading inputfile: " & errTxt
end if
end try
end repeat
end walkFile
on run (args)
set caller to class of args as string
set errorIndicator to 0
set destPath to POSIX path of (get path to home folder) & "export/photos/"
set photoCnt to 0
set startAlbum to missing value
set startPhoto to missing value
set match to 3
set dummy to false
set walkFileName to missing value
set walkFileOffset to 0
set infoFd to missing value
set infoFileName to missing value
set chooseDestinationFolder to "Select start folder (defaults to " & destPath & ")"
set chooseStart to "Give start album and photo"
set chooseDryRun to "dry-run"
if caller = "script" then
log "Running in ScriptEditor:" & name of me
set options to choose from list {chooseDestinationFolder, chooseStart, chooseDryRun} with title "Configure run" with prompt "Select options" with multiple selections allowed and empty selection allowed
if options contains chooseDryRun then set dummy to true
if options contains chooseDestinationFolder then
try
set destFolder to choose folder with prompt "Choose export directory or cancel for default location" default location (get path to home folder as alias)
set destPath to POSIX path of destFolder
end try
end if
if options contains chooseStart then
set res to display dialog "Enter Id of album to start" default answer "" buttons {"OK"} default button 1
if length of text returned of res > 0 then
set startAlbum to text returned of res
set match to 0
end if
if match = 0 then
set res to display dialog "Enter id of photo to resume after (leave empty to start with album)" default answer "" buttons {"OK"} default button 1
if length of text returned of res > 0 then
set startPhoto to text returned of res
end if
end if
end if
else if caller = "list" then
log "Running on the command line:" & name of me
set cnt to 1
repeat while cnt ≤ length of args
if item cnt of args = "-t" then
set cnt to (cnt + 1)
set destPath to item cnt of args
set cnt to (cnt + 1)
else if item cnt of args = "-a" then
set match to 0
set cnt to (cnt + 1)
set startAlbum to item cnt of args
else if item cnt of args = "-p" then
set cnt to (cnt + 1)
set startPhoto to item cnt of args
else if item cnt of args = "-h" then
return name of me & " [-t target path] [-a start album id] [-a start photo id] [-d] [-i info file path]" & linefeed ¬
& name of me & "[-t target path] -f info file [-o offset]" & linefeed ¬
& name of me & " -h .. this help"
else if item cnt of args = "-d" then
set dummy to true
else if item cnt of args = "-i" then
set cnt to (cnt + 1)
set infoFileName to item cnt of args
else if item cnt of args = "-f" then
set cnt to (cnt + 1)
set walkFileName to item cnt of args
else if item cnt of args = "-o" then
set cnt to cnt + 1
set walkFileOffset to item cnt of args
else
error "Invalid option:" & item cnt of args & " - use -h for help"
end if
set cnt to (cnt + 1)
end repeat
end if
set mySettings to "Destination directory:" & destPath
if startAlbum is not missing value then set mySettings to mySettings & linefeed & tab & "Start album id:" & tab & startAlbum
if startPhoto is not missing value then set mySettings to mySettings & linefeed & tab & "Start photo id:" & tab & startPhoto
if infoFileName ≠ missing value then
set mySettings to mySettings & linefeed & tab & "Write file '" & infoFileName & "'" & linefeed
set dummy to true
end if
if dummy then set mySettings to mySettings & linefeed & tab & "Dry run only"
if caller = "script" then
display dialog mySettings with title "Start exporting?" with icon note
else if caller = "list" then
log mySettings
else
error "Internal error - caller interface unknown"
end if
if infoFileName ≠ missing value then
set infoFd to open for access POSIX file infoFileName with write permission
set eof infoFd to 1
end if
if walkFileName ≠ missing value then
walkFile(walkFileName, walkFileOffset)
else
walkFolder(application "Photos", destPath)
end if
if infoFd ≠ missing value then close access infoFd
return "Done!"
end run

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

"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.

Applescript: Passing track metadata from text file to broadcast streaming app

I know virtually nothing about AppleScript and could do with some help.
While running, my broadcast playout system (MegaSeg) writes details of the track currently playing to a textfile called "NowPlaying" which contains solely the following contents, each on its own line and without square brackets:
Title: [title]
Artist: [artist]
Album: [album]
Time: [time in some format or other]
I want to pass this to my broadcast streamer LadioCast, which can be addressed via AppleScript, so that it sends info on what is playing to the streaming server.
MegaSeg doesn't support Applescript calls like 'tell application "MegaSeg"... set trackName to name of current track' so I can't do it that way. I have no idea about how to do this.
If we imagine that I was able to grab that info, this is what I would do with it:
set lastName to ""
set lastArtist to ""
set lastAlbum to ""
repeat
** insert missing file reading section here
** in the following, "title", "artist" and "album" are from the text file **
set trackName to title
set trackArtist to artist
set trackAlbum to album
** end of missing section
if trackName is not lastName and trackArtist is not lastArtist and trackAlbum is not lastAlbum then
set lastName to trackName
set lastArtist to trackArtist
set lastAlbum to trackAlbum
tell application "LadioCast"
set metadata song to trackName & " – " & trackArtist & " – " & trackAlbum
end tell
end if
delay 15
end repeat
Thanks in advance.
--Richard E
===
I tried the suggestions from #DigiMonk and they were very helpful, but not all of them worked. First, the file is in a specific location and not on the desktop; second, I could not get 'trim_line' to work at all - I got "script does not understand trim_line message". However the text in the file seems to be already trimmed.
The script below nearly works; when run from the Applescript Editor it does seem to get the Title, Artist and Album into the variables. However when I try running it from LadioCast I get an EOF -39 error. More importantly, as soon as I run this, MegaSeg stops updating the file, forever. I am presumably locking the file to read from it and stopping MegaSeg writing to it. How do I avoid this?
set lastName to ""
set lastArtist to ""
set lastAlbum to ""
set lastTime to ""
set trackName to ""
set trackArtist to ""
set trackAlbum to ""
set sourcePath to ""
repeat
set sourcePath to open for access file "Library:MegaSeg User Data:Logs:Logs for MegaSeg System (4):NowPlaying"
set thisText to read sourcePath as text
close access file "Library:MegaSeg User Data:Logs:Logs for MegaSeg System (4):NowPlaying"
set the paragraphList to every paragraph of thisText
repeat with i from 1 to number of items in paragraphList
set thisItem to item i of paragraphList
if thisItem starts with "Title:" then
set x to the offset of "Title:" in thisItem
set trackName to (text (x + 6) thru -1 of thisItem)
else if thisItem starts with "Artist:" then
set x to the offset of "Artist:" in thisItem
set trackArtist to (text (x + 7) thru -1 of thisItem)
else if thisItem starts with "Album:" then
set x to the offset of "Album:" in thisItem
set trackAlbum to (text (x + 6) thru -1 of thisItem)
end if
end repeat
if trackName is not lastName and trackArtist is not lastArtist and trackAlbum is not lastAlbum then
set lastName to trackName
set lastArtist to trackArtist
set lastAlbum to trackAlbum
tell application "LadioCast"
set metadata song to trackName & " – " & trackArtist & " – " & trackAlbum
end tell
end if
delay 15
end repeat
May be this helps: (feed sourcePath with the path to your NowPlaying.txt file)
set sourcePath to (path to desktop) & "NowPlaying.txt" as text
set thisText to my readFile(sourcePath)
set the paragraphList to every paragraph of thisText
set lastName to ""
set lastArtist to ""
set lastAlbum to ""
set lastTime to ""
repeat with i from 1 to number of items in paragraphList
set thisItem to item i of paragraphList
--log thisItem
if thisItem starts with "Title:" then
set x to the offset of "Title:" in thisItem
set y to (text (x + 6) thru -1 of thisItem)
set lastName to my trim_line(y, " ", 2)
else if thisItem starts with "Artist:" then
set x to the offset of "Artist:" in thisItem
set y to (text (x + 7) thru -1 of thisItem)
set lastArtist to my trim_line(y, " ", 2)
else if thisItem starts with "Album:" then
set x to the offset of "Album:" in thisItem
set y to (text (x + 6) thru -1 of thisItem)
set lastAlbum to my trim_line(y, " ", 2)
else if thisItem starts with "Time:" then
set x to the offset of "Time:" in thisItem
set y to (text (x + 5) thru -1 of thisItem)
set lastTime to my trim_line(y, " ", 2)
end if
end repeat
log "lastName = '" & lastName & "'"
log "lastArtist = '" & lastArtist & "'"
log "lastAlbum = '" & lastAlbum & "'"
on readFile(thisFile)
set thisFile to thisFile as text
if thisFile is "" then return ""
try
set fi to open for access file the thisFile
set myData to read fi as text
close access file the thisFile
return myData
on error
try
close access file the thisFile
return ""
end try
end try
end readFile
on trim_line(this_text, trim_chars, trim_indicator)
-- 0 = beginning, 1 = end, 2 = both
set x to the length of the trim_chars
-- TRIM BEGINNING
if the trim_indicator is in {0, 2} then
repeat while this_text begins with the trim_chars
try
set this_text to characters (x + 1) thru -1 of this_text as string
on error
-- the text contains nothing but the trim characters
return ""
end try
end repeat
end if
-- TRIM ENDING
if the trim_indicator is in {1, 2} then
repeat while this_text ends with the trim_chars
try
set this_text to characters 1 thru -(x + 1) of this_text as string
on error
-- the text contains nothing but the trim characters
return ""
end try
end repeat
end if
return this_text
end trim_line

Applescript indesign to list all color swatches from multiple files

I am working on finishing up an script droplet to pull all used swatches out of multiple indesign documents that I drop on the script and list them. I am adapting this from a script I wrote to put all used swatches from a single file.
I have the basics down but have run into two problems and cant find an answer.
Problem 1: I need to add the line to "repeat until swatchCount < 2" but this would ad a repeat inside a repeat. Doesn't work.
Problem 2: (the biggest problem) The "colorList" needs to add the colors from each of the documents together into a single list. Since I have it on repeat it is only listing the colors from the last processed file. How do I have the list save each files color and then list them all?
I would appreciate any help I have hit a wall.
on open these_items
repeat with i from 1 to the count of these_items
set this_item to item i of these_items
set the item_info to info for this_item
set this_item to POSIX path of this_item
tell application "Adobe InDesign CC"
open this_item
tell active document
delete unused swatches
set allSwatches to every swatch
end tell
set swatchCount to count of allSwatches
set colorList to ""
-- repeat until swatchCount < 2
set thisSwatch to item swatchCount of allSwatches
try
set swatchProps to type of thisSwatch
set swatchProps to "Gradient"
on error
set swatchProps to "Color"
end try
if swatchProps = "Color" then
set swatchName to name of thisSwatch as string
set swatchType to model of thisSwatch as string
set swatchSpace to space of thisSwatch as string
set swatchColor to color value of thisSwatch
if swatchType contains "spot" then
set swatchType to "Spot"
else
if swatchType contains "prss" then
set swatchType to "Process"
end if
end if
if swatchSpace contains "RGB" then
set rrr to ((item 1 of swatchColor) as integer)
set ggg to ((item 2 of swatchColor) as integer)
set bbb to ((item 3 of swatchColor) as integer)
set swatchMix to (" value: R-" & rrr & " G-" & ggg & " B-" & bbb) as string
if swatchName does not contain "registration" then
set swatchItem to "Name: " & swatchName & swatchMix & " space: RGB" & " type: " & swatchType & return
else
set swatchItem to ""
end if
else
if swatchSpace contains "CMYK" then
set ccc to ((item 1 of swatchColor) as integer)
set mmm to ((item 2 of swatchColor) as integer)
set yyy to ((item 3 of swatchColor) as integer)
set kkk to ((item 4 of swatchColor) as integer)
set swatchMix to (" value: C-" & ccc & " M-" & mmm & " Y-" & yyy & " K-" & kkk) as string
if swatchName does not contain "registration" then
set swatchItem to "Name: " & swatchName & swatchMix & " space:CMYK" & " type: " & swatchType & return
else
set swatchItem to ""
end if
else
if swatchSpace contains "LAB" then
set lll to ((item 1 of swatchColor) as integer)
set aaa to ((item 2 of swatchColor) as integer)
set bbb to ((item 3 of swatchColor) as integer)
set swatchMix to (" value: L-" & lll & " A-" & aaa & " B-" & bbb) as string
if swatchName does not contain "registration" then
set swatchItem to "Name: " & swatchName & swatchMix & " space: LAB" & " type: " & swatchType & return
else
set swatchItem to ""
end if
end if
end if
end if
set colorList to colorList & swatchItem
end if
tell active document to close saving no
set swatchCount to swatchCount - 1
--end repeat
end tell
end repeat
set dragged_items to these_items as string
set old_delimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set theClientName to text item 2 of dragged_items
set theFileName to text item 5 of dragged_items
set AppleScript's text item delimiters to old_delimiters
set theOrderNumber to text 1 through 5 of theFileName
set headerInfo to "Client: " & theClientName & " Order # " & theOrderNumber & " File: " & theFileName
tell application "Adobe InDesign CC"
tell view preferences
set horizontal measurement units to inches
set vertical measurement units to inches
end tell
make new document with properties {document preferences:{page width:8.5, page height:11}}
tell view preferences
set horizontal measurement units to inches
set vertical measurement units to inches
end tell
tell active document
set zero point to {0, 0}
set properties of guide preferences to {guides shown:false}
set properties of text preferences to {show invisibles:false}
set properties of view preferences to {ruler origin:page origin, horizontal measurement units:inches, show frame edges:false, show rulers:true, vertical measurement units:inches}
tell layout window 1
zoom given fit page
set properties to {view display setting:typical, transform reference point:top left anchor}
end tell
make new text frame with properties {geometric bounds:{0.5, 0.5, 0.75, 8.0}, contents:headerInfo, color:"None"}
set paragraphCount to count of paragraphs of colorList
repeat until paragraphCount > 19
set colorList to colorList & "name: _________________________" & " value: ____________________" & " space: ______" & " type: _________" & return
set paragraphCount to count of paragraphs of colorList
end repeat
make new text frame with properties {geometric bounds:{4.5, 0.5, 20.0, 8.0}, contents:colorList, color:"None"}
tell text frame 1
tell every text to set point size to 10
tell every paragraph
make tab stop with properties {alignment:left align, position:2.875}
make tab stop with properties {alignment:left align, position:5.125}
make tab stop with properties {alignment:left align, position:6.375}
end tell
set geometric bounds to {1.0, 0.5, 11, 8.0}
end tell
end tell
end tell
end open
on run
-- Handle the case where the script is launched without any dropped files
open (choose file with multiple selections allowed)
end run
I have added the changes that you suggested "Darrick Herwehe" but am getting an error now on a different part.
Can’t get name of color id 344 of document id 30
--name of color id 344 of document id 29 of application "Adobe InDesign CC"--
getting this in reference to: set swatchName to name of thisSwatch
on open these_items
set colorList to ""
set swatchItem to ""
repeat with i from 1 to the count of these_items
--Get swatches from document start
set this_item to item i of these_items
set the item_info to info for this_item
set this_item to POSIX path of this_item
tell application "Adobe InDesign CC"
open this_item
tell active document
delete unused swatches
set allSwatches to every swatch
end tell
set swatchCount to count of allSwatches
repeat until swatchCount < 2
repeat with j from 1 to (count allSwatches)
set thisSwatch to item swatchCount of allSwatches
try
set swatchProps to type of thisSwatch
set swatchProps to "Gradient"
on error
set swatchProps to "Color"
end try
if swatchProps = "Color" then
set swatchName to name of thisSwatch
set swatchType to model of thisSwatch
set swatchSpace to space of thisSwatch
set swatchColor to color value of thisSwatch
if swatchType contains "spot" then
set swatchType to "Spot"
else
if swatchType contains "prss" then
set swatchType to "Process"
end if
end if
if swatchSpace contains "RGB" then
set rrr to ((item 1 of swatchColor) as integer)
set ggg to ((item 2 of swatchColor) as integer)
set bbb to ((item 3 of swatchColor) as integer)
set swatchMix to (" value: R-" & rrr & " G-" & ggg & " B-" & bbb) as string
if swatchName does not contain "registration" then
set swatchItem to "Name: " & swatchName & swatchMix & " space: RGB" & " type: " & swatchType & return
else
set swatchItem to ""
end if
else
if swatchSpace contains "CMYK" then
set ccc to ((item 1 of swatchColor) as integer)
set mmm to ((item 2 of swatchColor) as integer)
set yyy to ((item 3 of swatchColor) as integer)
set kkk to ((item 4 of swatchColor) as integer)
set swatchMix to (" value: C-" & ccc & " M-" & mmm & " Y-" & yyy & " K-" & kkk) as string
if swatchName does not contain "registration" then
set swatchItem to "Name: " & swatchName & swatchMix & " space:CMYK" & " type: " & swatchType & return
else
set swatchItem to ""
end if
else
if swatchSpace contains "LAB" then
set lll to ((item 1 of swatchColor) as integer)
set aaa to ((item 2 of swatchColor) as integer)
set bbb to ((item 3 of swatchColor) as integer)
set swatchMix to (" value: L-" & lll & " A-" & aaa & " B-" & bbb) as string
if swatchName does not contain "registration" then
set swatchItem to "Name: " & swatchName & swatchMix & " space: LAB" & " type: " & swatchType & return
else
set swatchItem to ""
end if
end if
end if
end if
tell active document to close saving no
set colorList to colorList & swatchItem
end if
set swatchCount to swatchCount - 1
end repeat
end repeat
end tell
end repeat
set dragged_items to these_items as string
set old_delimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set theClientName to text item 2 of dragged_items
set theFileName to text item 5 of dragged_items
set AppleScript's text item delimiters to old_delimiters
set theOrderNumber to text 1 through 5 of theFileName
set headerInfo to "Client: " & theClientName & " Order # " & theOrderNumber & " File: " & theFileName
tell application "Adobe InDesign CC"
tell view preferences
set horizontal measurement units to inches
set vertical measurement units to inches
end tell
make new document with properties {document preferences:{page width:8.5, page height:11}}
tell view preferences
set horizontal measurement units to inches
set vertical measurement units to inches
end tell
tell active document
set zero point to {0, 0}
set properties of guide preferences to {guides shown:false}
set properties of text preferences to {show invisibles:false}
set properties of view preferences to {ruler origin:page origin, horizontal measurement units:inches, show frame edges:false, show rulers:true, vertical measurement units:inches}
tell layout window 1
zoom given fit page
set properties to {view display setting:typical, transform reference point:top left anchor}
end tell
make new text frame with properties {geometric bounds:{0.5, 0.5, 0.75, 8.0}, contents:headerInfo, color:"None"}
set paragraphCount to count of paragraphs of colorList
repeat until paragraphCount > 19
set colorList to colorList & "name: _________________________" & " value: ____________________" & " space: ______" & " type: _________" & return
set paragraphCount to count of paragraphs of colorList
end repeat
make new text frame with properties {geometric bounds:{4.5, 0.5, 20.0, 8.0}, contents:colorList, color:"None"}
tell text frame 1
tell every text to set point size to 10
tell every paragraph
make tab stop with properties {alignment:left align, position:2.875}
make tab stop with properties {alignment:left align, position:5.125}
make tab stop with properties {alignment:left align, position:6.375}
end tell
set geometric bounds to {1.0, 0.5, 11, 8.0}
end tell
end tell
end tell
end open
on run
-- Handle the case where the script is launched without any dropped files
open (choose file with multiple selections allowed)
end run
If you have any insight I would much appriciate it.
Thanks again
Matt
You can nest repeat loops. It's not a problem, it's done all the time. You just have to define a different iteration variable from your first repeat loop. In your case, I would use j.
As for getting all of the colors into one list, you neeed to define colorList outside your first repeat loop. That way it doesn't get overwritten during each iteration.
An outline for that would be:
on open these_items
set colorList to ""
repeat with i from 1 to the count of these_items
-- [ Get swatches from the document ]
repeat with j from 1 to (count allSwatches)
-- [ Process your swatches ]
set colorList to colorList & swatchItem
end repeat
end repeat
end open

Resources