Applescript convert variable into a string with commas for CSV - macos

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

Related

How to set text in Pages with 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!

How to automate the creation of a folder, and a text file within it from CSV data

For each line of a delimited text file, I want to have Automator create a folder named from a field of the source file.
Then in that folder, create a text file named "file.json" with the contents being a string of data from another field of the source file.
The CSV file would have about 100 entries, and probably just two fields like
folderName | { JSON was here }
How would I go about doing that?
I don't know automator so well, but the following AppleScript should do what you want. Does not have much in the way of error checking.
try
set src to (choose file with prompt "choose your input")
set o to (open for access src)
set inputStr to (read o)
close access o
end try
set fldrOut to (choose folder with prompt "choose your output folder")
set atids to AppleScript's text item delimiters
set AppleScript's text item delimiters to "|"
repeat with p in (paragraphs of inputStr)
set folderName to text item 1 of (p as string)
set jsonString to text item 2 of (p as string)
tell application "Finder"
set fldr to (make new folder at fldrOut with properties {name:folderName})
end tell
set pth to (fldrOut as string) & folderName & ":file.json"
set outFile to (open for access pth with write permission)
write jsonString to outFile starting at 0
close access outFile
end repeat
set AppleScript's text item delimiters to atids

applescript list of droppeditems not displaying properly

i have the following code:
on open theDroppedItems
set PosixList to {}
repeat with a from 1 to length of theDroppedItems
set theCurrentDroppedItem to item a of theDroppedItems
set PosixPath to convertPathToPOSIXString(theCurrentDroppedItem)
copy PosixPath to the end of the PosixList
end repeat
display dialog convertListToString(PosixList, space)
end open
on convertPathToPOSIXString(thePath)
tell application "System Events"
try
set thePath to path of disk item (thePath as string)
on error
set thePath to path of thePath
end try
end tell
return POSIX path of thePath
end convertPathToPOSIXString
on convertListToString(theList, theDelimiter)
set AppleScript's text item delimiters to theDelimiter
set theString to theList as string
set AppleScript's text item delimiters to ""
return theString
end convertListToString
I expect it to allow me to drop a file onto the applescript (saved as an application) and display a list similar to /Users/adam/a.txt /users/adam/b.txt however, it gives me 2 dialog's, 1 with the path to the first file, then the second with the path to the second file. What am i missing?
thanks!
I think this shortened version is more along the lines of what you are looking for?
on open theDroppedItems
set posixList to {}
repeat with a from 1 to length of theDroppedItems
set theCurrentDroppedItem to item a of theDroppedItems
set posixPath to POSIX path of theCurrentDroppedItem
set end of posixList to (posixPath & linefeed & linefeed)
end repeat
display dialog posixList as string
end open

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