I am trying to write a little script to replace characters in a text with AppleScript.
Unfortunately upper and lower case is not being recognized by AppleScript.
Any idea how to take upper and lower case into account?
set input to "ApeAlexaBobBorder"
set the clipboard to replaceText(input as string)
on replaceText(textString)
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to "A"
set textString to text items of textString
set AppleScript's text item delimiters to ", A"
set textString to "" & textString
set AppleScript's text item delimiters to prevTIDs
return textString
end replaceText
You can group the comparison(s) in a considering statement, for example:
considering case
set the clipboard to replaceText(input as string)
end considering
See the AppleScript Language Guide for more information.
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
This code gets two bits of two and copies them to the clipboard.
One bit of text is html. Before stripping out all the html I want to replace the tags with empty lines.
I can't get the replaced correctly, but the rest of the code works.
Any idea what I am doing wrong. I also tried using return instead of linefeed.
tell application "GarageSale 7"
repeat with theListing in (get selected ebay listings)
set des to get the description of theListing
set comment to get private comment of theListing
end repeat
end tell
set theText to des
to searchReplace(thisText, "<br>", linefeed)
set AppleScript's text item delimiters to searchTerm
set thisText to thisText's text items
set AppleScript's text item delimiters to replacement
set thisText to "" & thisText
set AppleScript's text item delimiters to {""}
return thisText
end searchReplace
on removeMarkupFromText(theText)
set tagDetected to false
set theCleanText to ""
repeat with a from 1 to length of theText
set theCurrentCharacter to character a of theText
if theCurrentCharacter is "<" then
set tagDetected to true
else if theCurrentCharacter is ">" then
set tagDetected to false
else if tagDetected is false then
set theCleanText to theCleanText & theCurrentCharacter as string
end if
end repeat
return theCleanText
end removeMarkupFromText
get the clipboard
set the clipboard to removeMarkupFromText(theText) & comment
You've defined a handler called searchReplace(), but you never actually use it. That's why your script isn't replacing the <br> tags.
Firstly, you want to define the handler properly. It should take arguments that are represented by variables; currently, your last two arguments are specific values:
to searchReplace(thisText, "<br>", linefeed)
Here's a suggested edit:
to searchReplace(thisText, searchTerm, replacement)
set my text item delimiters to searchTerm
set thisText to thisText's text items
set my text item delimiters to replacement
set thisText to thisText as text
set my text item delimiters to {""}
return thisText
end searchReplace
Then you can call it from your script like so:
tell application "GarageSale 7"
repeat with theListing in (get selected ebay listings)
set des to get the description of theListing
set comment to get private comment of theListing
end repeat
end tell
set theText to searchReplace(des, "<br>", linefeed)
set the clipboard to removeMarkupFromText(theText) & comment
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
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