Prefixing an Applescript text items list - applescript

I have the following code, which prompts user for a comma separated list of places:
set AppleScript's text item delimiters to {","}
set thePlaces to the text items of the text returned of (display dialog "Insert referenced places separated by commas" default answer "")
This will result on a list with several items ("Paris","London", ...).
My intention is to prefix every item of this list with a string (for eg. "plc:".
In the end, I would like the list to be composed by items such as:
"plc:Paris", "plc:London".
Have been trying but with no luck so far. Can anyone point me in the right direction?
Thanks.

Looks a little brutal, but works as wished:
repeat with i from 1 to count thePlaces
set item i of thePlaces to "plc:" & item i of thePlaces
end repeat
The repeat loop loops through the items and add "plc:" in front of the content...
Enjoy, Michael / Hamburg

This is how you can do it with text item delimiters, we box each item, with a unique value for the front, and one for the end, so that we can differentiate between the two. No use for this with so small lists, really. I just wanted to show you how it can be done.
set astid to text item delimiters
set the places to "Paris,London,Rome"
set text item delimiters to ","
set lstItms to text items of the places
-- we "box" the text items, so that every one is prepended with a return, and has a linefeed appended to it.
set text item delimiters to return & linefeed
set places to lstItms as text
set text item delimiters to astid
set places to linefeed & places & return
-- our list is in shape, time to do the actual replacement.
set text item delimiters to linefeed
set lstItms to text items of places
set text item delimiters to "plc:"
set places to lstItms as text
set text item delimiters to return
set lstItms to text items of places
set text item delimiters to astid
log item 1 of lstItms
(*plc:Paris*)

Related

How to use Applescript text item delimiters while ignoring a specific character

I would like to separate every URL in a text list with a comma in an Applescript application. An example input would be:
alloresto.fr eat.ch eatnow.com.au just-eat.ca just-eat.co.uk
Using Applescript's text item delimiters, I get partial success:
set enterDomains to myTextField's stringValue() as text -- gets text from text field in my xib window
set AppleScript's text item delimiters to ", "
set theResults to every word of enterDomains as string
set AppleScript's text item delimiters to ""
alloresto.fr, eat.ch, eatnow.com.au, just, eat.ca, just, eat.co.uk
But this breaks every domain with a - in it since I have it set to every word. Is it possible to ignore the - character when using Applescript's text item delimiters?
I know my issue is located with every word of enterDomains since hyphenated domains contain more than one word but when I change this line to something like text of enterDomains, it returns me the same list of domains as a result without any added commas.
Ideas or suggestions?
words of... versus text items of...
words of... functions independently of text item delimiters, so will always split a string in the same way.
text item delimiters allows you to specify one or more phrases at which to delimit a string to be separated into a list of text items. It also determines how a list of text items are joined together, so will be significant in any instance where a list object is coerced into text (or string) object.
To split a string at every occurrence of a space character, and only a space character:
set enterDomains to "alloresto.fr eat.ch eatnow.com.au just-eat.ca just-eat.co.uk"
set my text item delimiters to space
set theResults to text items in enterDomains
--> {"alloresto.fr", "eat.ch", "eatnow.com.au", "just-eat.ca", "just-eat.co.uk"}
Then, to join this list of text items into a string, delimited by a comma-space:
# ...Cont'd from the previous code block
set my text item delimiters to ", "
return theResults as text
--> "alloresto.fr, eat.ch, eatnow.com.au, just-eat.ca, just-eat.co.uk"
text item delimiters more generally
As I stated above, text item delimiters can actually be set to a list of several items, instead of just a single character or phrase. This causes a string to be split at every occurrence of every item in your specified list of text item delimiters, e.g.
set input to "The quick brown fox jumps over the lazy dog."
set my text item delimiters to {"i", space, "fox"}
get the text items of the input
--> {"The", "qu", "ck", "brown", "", "", "jumps", "over", "the", "lazy", "dog."}
The order of the delimiters does not matter here, because a string will be split using every delimiter acting in a simultaneous fashion.
However, when joining the list back up into a text object, only the first delimiter is used to glue the text items back together. In this case, it will be the "i":
# ...Cont'd from the previous code block
# result: {"The", "qu", "ck", "brown", "", "", "jumps", "over", "the", "lazy", "dog."}
return the result as text
--> "Theiquickibrowniiijumpsioveritheilazyidog."
Notice that when a string is split, every occurrence of a delimiter is deleted from the string; when it is joined, the first delimiter only is inserted in between each chunk of text. This is effectively replacing bits of text with something else.
Text replacement using text item delimiters (An introduction)
In your specific case, your task can be summarised as needing to replace the spaces in your string with comma-spaces. So we can do this in a single move, by setting the text item delimiters such that space characters are deleted, and ", " is inserted during concatenation:
set enterDomains to "alloresto.fr eat.ch eatnow.com.au just-eat.ca just-eat.co.uk"
set my text item delimiters to {", ", space}
return the text items of enterDomains as text
--> "alloresto.fr eat.ch, eatnow.com.au just-eat.ca, just-eat.co.uk"
There are other characteristics and peculiarities of text item delimiters that I've talked about in other answers on Stack Overflow, which you are free to search for. But, for the vast majority of use cases, the information above is the most relevant.
AppleScript-ObjC
...because I saw your use of stringValue() and reference to a .xib window, I'll quickly give you the AppleScriptObjC equivalent for some of the above scenarios.
As I'm sure you'll know, all of the examples will only work if the script in which they appear has the following initial lines of code:
use framework "Foundation"
# use scripting additions -- if Standard Additions are needed
property this : a reference to the current application
property NSArray : a reference to NSArray of this
property NSString : a reference to NSString of this
I'll use the assignment-declaration for enterDomains in your code as a starting point, but instead of coercing it to text, I'll leave it in as the cocoa referenced object returned by stringValue() (the -- comments out the coercion):
set enterDomains to myTextField's stringValue() -- as text
Therefore, enterDomains now contains an instance of an NSString class value object. So:
Splitting a string:
set theResults to enterDomains's componentsSeparatedByString:space
Joining a list of strings:
theResults's componentsJoinedByString:", "
Replacing every space with a comma-space:
my TextField's stringValue()'s stringByReplacingOccurrencesOfString:space withString:", "
return the result as text
See Apple's documentation on the NSString class for more stuff.
Consider this example:
set s to "alloresto.fr eat.ch eatnow.com.au just-eat.ca just-eat.co.uk"
set AppleScript's text item delimiters to " "
set theResults to every text item of s
-- {"alloresto.fr", "eat.ch", "eatnow.com.au", "just-eat.ca", "just-eat.co.uk"}
set AppleScript's text item delimiters to ""
Go ye and do likewise.

How do I add a comma after each character in a string using AppleScript?

I have a string with periods and numbers.
Ex. 4..1...68875....9.....92.7...42.9..6..9...1..6..7.59...8.64.....5....71494...3..2
I am trying to add a comma after each character to get this:
4,.,.,1,.,.,.,6,8,8,7,5,.,.,.,.,9,.,.,.,.,.,9,2,.,7,.,.,.,4,2,.,9,.,.,6,.,.,9,.,.,.,1,.,.,6,.,.,7,.,5,9,.,.,.,8,.,6,4,.,.,.,.,.,5,.,.,.,.,7,1,4,9,4,.,.,.,3,.,.,2
I have about 50 of these lines to convert. Is there a quicker way to do so with AppleScript?
All the searches return how to split with delimiters but I have no idea what terminology I am looking at and what phrase I need to use to get the actual answer.
I just want to add a comma after each character.
Thanks for any help.
Try this:
set myVar to "4..1...68875....9.....92.7...42.9..6..9...1..6..7.59...8.64.....5....71494...3..2"
set {saveTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {""}}
set myList to text items of myVar as list
set AppleScript's text item delimiters to {","}
set myString to myList as string
set AppleScript's text item delimiters to saveTID

Bugs with extracting text between two strings AppleScript method

Newbie to AppleScript here, I've learned the following method from other online sources that allows me to extract a string that is between two strings I can define, see code:
to ExText(searchText, startText, endText)
set spaceholder to "x"
set searchText to ("x" & searchText)
set AppleScript's text item delimiters to spaceholder
set endItems to text item -1 of searchText
set AppleScript's text item delimiters to endText
set beginningToEnd to text item 1 of endItems
set AppleScript's text item delimiters to startText
set finalText to (text items 2 thru -1 of beginningToEnd) as text
set AppleScript's text item delimiters to ""
return finalText
end ExText
I'm using this method to extract user's first and last name from an html string obtained by running Javascript id search. I'll do
Set SourceString to "<span>firstname lastname</span>"
ExText(SourceString, "<span>", "</span>")
It works 90% of the time, but in a few instances, depending on the user's name, it'll get an error with this message
Can’t make text items 2 thru -1 of "(part of the name)" into type text.
Here are some sample names that'll break this method
<span>Xu Chang</span>
<span>Maxim Smith</span>
In further testing, I confirmed that any names containing the letter "X" would fail this method. I've examined the actual source strings side by side with the names that contain X and the ones that don't, so it's not the source string but the method itself.
It's worth noting that this error is reproducible everytime.
A rather bizarre behavior. Any ideas?
changing the variable of spaceholder from "x" to "xx" should work
to ExText(searchText, startText, endText)
set spaceholder to "xx"
set searchText to ("xx" & searchText)
set AppleScript's text item delimiters to spaceholder
set endItems to text item -1 of searchText
set AppleScript's text item delimiters to endText
set beginningToEnd to text item 1 of endItems
set AppleScript's text item delimiters to startText
set finalText to (text items 2 thru -1 of beginningToEnd) as text
set AppleScript's text item delimiters to ""
return finalText
end ExText

Filter clipboard and save as data list

I'm trying to get an AppleScript to find somme keyword from the clipboard and list them in a new clipboard
e.g : I copy this line in the clipboard "order KAFGEFEF price 999 date 17 order KADFSDGS price 874 date 18"`
and the result will be
K1AFGE2FEF
K1ADFSD2GS
or even beter
K1AFGE2FEF : 999
K1ADFSD2GS : 17
the data I want to collect always start with "K1...." and have 10 characteres.
I actually had a old Javascript which are kind of doing the trick but I need to use AppleScript instead.
I really not sure where to start here, maybe I shoud start something around pbcopy and egrep ?
hope that's make sense.
Kind regards.
It is not clear from your question exactly how your clipboard data is structured or what your desired output is. For starters, here is an Applescript solution that will extract order, price, and date values from the clipboard. It assumes that order, price, and date are always grouped together in that specific order, and that there can be multiple order-price-date groups in a single line of text on the clipboard. For example:
order K1AFGE2FEF price 999 date 17 order K1ADFSD2GS price 874 date 18
Then the following Applescript will extract each order, price, and date triplet and save it as a three-item sublist in a master list:
set masterList to {}
set tid to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to "order "
repeat with i in (get (the clipboard)'s text items 2 thru -1)
tell i's contents
try
set currOrder to first word
set AppleScript's text item delimiters to "price "
set currPrice to (get its text item 2)'s first word
set AppleScript's text item delimiters to "date "
set currDate to (get its text item 2)'s first word
if (currOrder starts with "K1") and (currOrder's length = 10) then set end of masterList to {currOrder, currPrice, currDate}
end try
end tell
end repeat
end try
set AppleScript's text item delimiters to tid
return masterList -- {{"K1AFGE2FEF", "999", "17"}, {"K1ADFSD2GS", "874", "18"}}
The master list can then be processed further into whatever output you desire.

Send email from clipboard without opening mail.app -- with conditions

I asked the question Send email from clipboard without opening mail.app and got the code
set a to "myemail#mail.com"
tell application "Mail"
tell (make new outgoing message)
set subject to (the clipboard)
set content to "content"
make new to recipient at end of to recipients with properties {address:a}
send
end tell
end tell
now I wonder, how could I have a script that do the same thing, but modifies it like this: if the Subject is the first 10 words, and iff the clipboard har more than 10 words, then the clipboard is cut off. For example like this "hello there baby this is a long message sent with... [see notes]" and then the enitre message (i.e. "hello there baby this is a long message sent with my new email, see you.") is in the content of the email.
Replace the set subject ... and set content ... lines in your script with the following:
if (count of words of (the clipboard)) is greater than 10 then
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set subject to ((words 1 through 10 of (the clipboard)) & "... [see notes]") as text
set AppleScript's text item delimiters to oldDelims
set content to (the clipboard)
else
set subject to (the clipboard)
set content to "content"
end if
Links to references:
count of gives the number of elements in a list
words of splits a text string into a list, with each element representing a word
AppleScript's text item delimiters can be manipulated to help split and join lists with different delimiters
the through keyword can be used to get a subrange of items from a list
#adayzdone has a good point - sometimes using words of to split a string to a list then reassembly with text item delimiters can mess up the input data. You could do this instead:
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set cblist to text items of (the clipboard)
set AppleScript's text item delimiters to oldDelims
if (count of cblist) is greater than 10 then
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set subject to ((items 1 through 10 of cblist) & "... [see notes]") as text
set AppleScript's text item delimiters to oldDelims
set content to (the clipboard)
else
set subject to (the clipboard)
set content to "content"
end if

Resources