I was wondering if there is a short way to convert an AppleScript list into a string separating each item. I can achieve this in a way which is more lengthy that I would like, so I wondered if there is a simple way to achieve this. Basically, I would like to take a list such as {1,2,3} and convert it in to a string "1, 2, 3". I can do something like below, but that results in a comma following the resulting string:
set myList to {"1.0", "1.1", "1.2"}
set Final to ""
if (get count of myList) > 1 then
repeat with theItem in myList
set Final to Final & theItem & ", "
end repeat
end if
There is a short way, it's called text item delimiters
set myList to {"1.0", "1.1", "1.2"}
set saveTID to text item delimiters
set text item delimiters to ", "
set Final to myList as text
set text item delimiters to saveTID
Create your own snippet for this
Convert a list to a string is so frequent that you better create a subroutine.
on list2string(theList, theDelimiter)
-- First, we store in a variable the current delimiter to restore it later
set theBackup to AppleScript's text item delimiters
-- Set the new delimiter
set AppleScript's text item delimiters to theDelimiter
-- Perform the conversion
set theString to theList as string
-- Restore the original delimiter
set AppleScript's text item delimiters to theBackup
return theString
end list2string
-- Example of use
set theList to {"red", "green", "blue"}
display dialog list2string(theList, ", ")
display dialog list2string(theList, "\n")
Related
I asked and read different topic about this in the last month, but still didn't get it
I want to extract some data from a website using Applescript or something simular
With this script
to getInputByClass2(theClass, num) -- defines a function with two inputs, theClass and num
tell application "Safari" --tells AS that we are going to use Safari
set input to do JavaScript "
document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1 -- uses JavaScript to set the variable input to the information we want
end tell
return input --tells the function to return the value of the variable input
end getInputByClass2
-- start here
getInputByClass2("field type-string field-accountname", 0)
set theText to Unicode text
set theText to getInputByClass2("field type-string field-accountname", 0)
I have this result :
"<div class=\"content\"><div class=\"related-orders-indicator component\" id=\"XXXX-BBBB-CCCC-DDDD-EEEE\" data-bad=\"0\" data-good=\"51\" data-total=\"51\">
<label class=\"count\" style=\"display: block;\">51</label>
</div><label class=\"name\">accountname</label><span class=\"value\">1985</span><div class=\"confirmation\"><input type=\"checkbox\" class=\"tri-state confirmation square-confirmation-style\"></div></div>"
How can I have only the account name as a variable, in this example 1985.
Many thanks in advance
You can do it with text item delimiters:
property leftEdge : "class=\"value\">"
property rightEdge : "</span>"
set theSource to "<div class=\"content\"><div class=\"related-orders-indicator component\" id=\"XXXX-BBBB-CCCC-DDDD-EEEE\" data-bad=\"0\" data-good=\"51\" data-total=\"51\"><label class=\"count\" style=\"display: block;\">51</label></div><label class=\"name\">accountname</label><span class=\"value\">1985</span><div class=\"confirmation\"><input type=\"checkbox\" class=\"tri-state confirmation square-confirmation-style\"></div></div>"
set saveTID to text item delimiters
set text item delimiters to leftEdge
set classValue to text item 2 of theSource
set text item delimiters to rightEdge
set theResult to text item 1 of classValue
set text item delimiters to saveTID
theResult
I need to have an AppleScript that will edit the contents of a given text file (generic structure) and delete the 5th character through 8th character, leave characters 9-20, and delete characters 21-32. For example:
Say this is my text file:
"
Qt&:$yp$shshshahahah$jsjsjajssjh
"
(Single line)
I would need to delete starting from the first $ to the next $, and then delete everything after (including) the last $. In this example, the end result would be this:
Qt&:shshshahahah
Thanks,
Isaac D'Keefe
You could run a command like this in Terminal:
sed -Ei '' 's/(.{4}).{4}(.{12}).*/\1\2/' ~/Documents/file
Or if you need to use AppleScript:
set p to "/Users/username/Documents/file"
set input to read p as «class utf8»
set input to text 1 thru 4 of input & text 9 thru 20 of input
set fd to open for access p with write permission
set eof fd to 0
write input to fd as «class utf8»
close access fd
I would need to delete starting from the first $ to the next $, and
then delete everything after (including) the last $. In this example,
the end result would be this:
Based on these conditions you can split an string up into chunks using a delimiter/separator. Then to my understanding you only want to keep the odd items and remove the even ones (by index). So the following script will work as you described rather than working on the index of the characters.
set theString to " Qt&:$yp$shshshahahah$jsjsjajssjh "
set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "$"}
set textItems to every text item of theString
set AppleScript's text item delimiters to oldTID
set filteredTextItems to {}
repeat with x from 1 to count textItems by 2
set end of filteredTextItems to item x of textItems
end repeat
return filteredTextItems as string
I am working on a script to have Finder get a file's location locally, replace spaces with %20, then append localhost:// so the transformed file name can be inserted into email.
I've have used the replacing spaces code structure suggested elsewhere on stackoverflow but the code is coming up with error 1721. I am not sure what I've done wrong.
The code for text substitution is:
on run {input, parameters}
set newString to {"localhost://"}
set aString to "/file name input/"
set aString to aString as text
set charToReplace to " "
set newChar to "%20"
repeat with i in aString
if (i as string) is charToReplace then
set end of newString to newChar
else
set end of newString to (i as string)
end if
end repeat
return input
end run
The output should be /file%20name%20input/
Thanks for any help you can offer.
Michael
Not sure on that 1721 error, but here is an alternate method for string search and replacing.
It uses AppleScript's text item delimiters. I think of this technique akin to the split().join() method to replace text in JavaScript.
set aString to "/file name input/"
set my text item delimiters to " "
set split_list to every text item of aString -- split in to list of everything between the spaces
set my text item delimiters to "%20"
set newString to (split_list as text) -- join, using the %20 as the delimter
set newString to "localhost://" & newString -- prepend your protocol string
Here is a standard version of a search and replace sub-routine for Applescript:
set encodedString to searchAndReplace(aString, space, "%20")
on searchAndReplace(myString, oldText, newText)
set AppleScript's text item delimiters to oldText
set myList to text items of myString
set AppleScript's text item delimiters to newText
set myString to myList as string
set AppleScript's text item delimiters to ""
return myString
end searchAndReplace
I would like to read in a CSV file in as a 2D array and then return it back to a CSV file.
Let's say that this is my CSV file. It is an excel file and the | implies the adjacent cell:
family | type
Doctor | Pediatrics
Engineer | Chemical
From what I understand, there are no arrays on applescript, just lists and records. If it would be better to do this with a XLSX file, please let me know.
Nigel's CSV-to-list converter is the best I have seen ...
http://macscripter.net/viewtopic.php?pid=125444#p125444
For your example, use these settings:
set csvText to "family | type
Doctor | Pediatrics
Engineer | Chemical"
csvToList(csvText, {separator:"|"}, {trimming:true})
v2
set csvText to read "/Users/user1385816/Desktop/yourfile.csv"
csvToList(csvText, {}, {trimming:true})
An array is just a list in applescript, so you want a 2d array or a list-of-lists in applescript-speak. If you understand applescript's text item delimiters then your task is just simple manipulation to convert strings into lists and vice versa. So I wrote you a couple handlers to make the task easy for you; textToTwoDArray() and twoDArrayToText(). This first example shows how to convert your string into a list-of-lists using textToTwoDArray().
NOTE: you have to be careful of the line endings in a text file because they can be either a carriage return (character id 13) or a line feed (character id 10). You can see I used character id 10 in my code but if you aren't getting the proper results try "13".
set fileText to "family | type
Doctor | Pediatrics
Engineer | Chemical"
textToTwoDArray(fileText, character id 10, " | ")
on textToTwoDArray(theText, mainDelimiter, secondaryDelimiter)
set {tids, text item delimiters} to {text item delimiters, mainDelimiter}
set firstArray to text items of theText
set text item delimiters to secondaryDelimiter
set twoDArray to {}
repeat with anItem in firstArray
set end of twoDArray to text items of anItem
end repeat
set text item delimiters to tids
return twoDArray
end textToTwoDArray
And here's how to convert a list-of-lists back into your string using twoDArrayToText().
set twoDArray to {{"family", "type"}, {"Doctor", "Pediatrics"}, {"Engineer", "Chemical"}}
twoDArrayToText(twoDArray, character id 10, " | ")
on twoDArrayToText(theArray, mainDelimiter, secondaryDelimiter)
set {tids, text item delimiters} to {text item delimiters, secondaryDelimiter}
set t to ""
repeat with anItem in theArray
set t to t & (anItem as text) & mainDelimiter
end repeat
set text item delimiters to tids
return (text 1 thru -2 of t)
end twoDArrayToText
So now all you have to do is figure out how to read and write to a text file with applescript. Good luck ;)
If your question is just about modeling your CSV data in AppleScript, the solution is to use a list of records like this:
set csvData to {{family:"Doctor", type:"Engineer"}, {family:"Engineer", type:"Chemical"}}
you can then read that out again inside a repeat with aRow in csvData loop.
A technical note: AppleScript lists are implemented as vectors, meaning they are ordered and can be accessed by numerical index (beginning at index 1) – they behave much like an array in most other languages.
In Applescript I have a string of single digit numbers:
0123456789
I want to go through that string and add a comma between each number, so it would show:
0,1,2,3,4,5,6,7,8,9
How can I do this with Applescript?
Notes
I want both the input and output to be of type "string" - not list.
The numbers will always be single digit numbers.
You can use a handy feature called AppleScript's text item delimiters, which allows you to break up (or "parse" in computer jargon) text into segments, and then extract data from those segments. They are the separators of text items in a piece of text. A simple example:
set prevTIDs to AppleScript's text item delimiters
set theString to "Don't-eat-the-yellow-snow"
set AppleScript's text item delimiters to "-" --tell AppleScript to break up strings at each occurrence of a hyphen '-' in a given string
set these_items to every text item of theString
//--> {"Don't", "eat", "the", "yellow", "snow"}
set AppleScript's text item delimiters to prevTIDs
return these_items
It is always considered good practice when working with text item delimiters to restore the original delimiters; once you change the delimiters, they will globally affect the running environment until the process is closed and restarted.
Another use for text item delimiters is replacing words or characters in a given string:
set prevTIDs to AppleScript's text item delimiters
set theString to "Don't eat the yellow snow"
set AppleScript's text item delimiters to "yellow" --the word you want to replace
set temp to every text item of theString
//--> {"Don't eat the", "snow"}
set AppleScript's text item delimiters to "pink" --the replacement word
set theString to temp as string
set AppleScript's text item delimiters to prevTIDs
return theString
//--> "Don't eat the pink snow"
However, note that this replaces every occurrence of the word "yellow". The reason I'm saying this is consider the following string:
If someone added one plus one, what would be the result?
If you wanted to replace the word "one" with the word "two", you would have to be sure to precede "two" with a space when creating the new delimiter, or your resulting string would be the following:
If sometwo added two plus two, what would be the result?
What you're trying to do is basically replacing empty strings with commas. All you need to do is follow these simple steps to do this:
Create a variable to store the current delimiters in
Create a variable to store your string in
Change the delimiter to an empty string ""
Coerce your string into a list (i.e. set the list to every text item of yourString)
Change the delimiter to a comma ,
Coerce your newly created list back into a string (i.e. set yourString to list as string)
Restore the old delimiters
return your string
The resulting code:
set prevTIDs to AppleScript's text item delimtiers
set myString to "0123456789"
set AppleScript's text item delimiters to ""
set the_list to every text item of myString
set AppleScript's text item delimiters to ","
set myString to the_list as string
set AppleScript's text item delimiters to prevTIDs
return myString
Happy coding! :)
Something like this?
set theNumber to "3452678190"
set AppleScript's text item delimiters to ","
set theItems to every character of theNumber as string
set AppleScript's text item delimiters to ""
return theItems