How would I turn a sentence entered by a user in AppleScript into separate variables for each word. For example, Lorem ipsum dolor sit amet would be split by the space into different variables, Ex. var1 = lorem, var2 = ipsum and so on. Below is what I've come up with so far, but I'm clearly getting nowhere.
set TestString to "1-2-3-5-8-13-21"
set myArray to my theSplit(TestString, "-")
on theSplit(theString, theDelimiter)
-- save delimiters to restore old settings
set oldDelimiters to AppleScript's text item delimiters
-- set delimiters to delimiter to be used
set AppleScript's text item delimiters to theDelimiter
-- create the array
set theArray to every text item of theString
-- restore the old setting
set AppleScript's text item delimiters to oldDelimiters
-- return the result
return theArray
end theSplit
Just use 'word' key word. Here is a small example of script :
set teststring to "1-2-3-5-8-13-21"
set Wordlist to words of teststring -- convert string to list of words
repeat with aword in Wordlist --loop through each word
log aword -- do what ever you need with aword
end repeat
Related
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")
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 have a string "remind me to eat dinner at 5:30"
I need to remove every word before "to" and every word after "at"
I already wrote a script to get the word number of them toNum and atNum
I can't figure out how to do this any ideas?
Thanks
Here is another approach:
set originString to "remind me to eat dinner at 5:30"
set {TID, text item delimiters} to {text item delimiters, {"to ", " at"}}
set resultString to text item 2 of originString
set text item delimiters to TID
Try this:
set originString to "remind me to eat dinner at 5:30"
set resultString to text toNum thru atNum of originString
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