Case sensitivity in AppleScript - applescript

I have a script that swaps out accented characters for their HTML equivalents, which works well but DOESN'T take into account the case sensitivity of the letters. This causes a lot of issues. I thought maybe the accented characters weren't being recognised as a case in the first place but after some testing I've discovered that that's not the issue since the same problem occurs with regular unaccented letters. I've tried adding considering case but that won't make it work. Here's a sample:
tell application "TextWrangler"
copy the contents of the selection of window 1 to meinText
set the contents of the selection of window 1 to asci2html(meinText) of me
end tell
on asci2html(derText)
considering case
tell application "TextWrangler"
set derText to replace "Á" using "Á" searchingString derText
set derText to replace "á" using "á" searchingString derText
set derText to replace "A" using "BIG" searchingString derText
set derText to replace "a" using "little" searchingString derText
return derText
end tell
end considering
end asci2html
That will test for an accented char as well as just the letter 'A' and if you run it on the following chars you'll see it doesn't differentiate case:
a
A
Á
á
Any thoughts? Thanks

I think I solved it, using some code I adapted from macosxautomation.com:
tell application "TextWrangler"
set outputText to "nuthing"
copy the contents of the selection of window 1 to meinText
set the contents of the selection of window 1 to asci2html(meinText) of me
return outputText
end tell
on replace_chars(this_text, search_string, replacement_string)
considering case
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end considering
end replace_chars
on asci2html(derText)
set the derText to replace_chars(derText, "a", "little")
set the derText to replace_chars(derText, "A", "BIG")
set outputText to derText
end asci2html
I'm sure it's not entirely efficient: I'm bouncing the values between a couple of different variables to side-step some errors that I couldn't figoure out, but it seems to be working anyway, as seen by the before-after screenshot! Any thoughts?
Screenshot-before-after

Related

Case Sensitive Search and Replace with AppleScript

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.

Applescript Replace all <br> tags with empty lines?

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

Apply noBreak AppleScript Illustrator

My goal here is to apply the no break parameter of Illustrator with AppleScript to two words in a text frame.
I'm able to detect the non-breaking space in a string. Then I need to apply the no break parameter to the word after and before the character 202 as no break space isn't supported by Illustrator
Open this Scriplet in your Editor:
set ourText to "Hello my friend Jon<non-breaking-space>Doe."
set findThis to (ASCII character 202)
set MW to words of ourText
repeat with aWord in MW
if findThis is in aWord then
set myWord to aWord
exit repeat
end if
end repeat
myWord
--> display: Jon Doe
Then I would like to search in the text frame for "Jon Doe" apply the no break parameter. I tried manually in Illustrator, this would work.
Your script doesn’t work because you are building a list of words. Spaces (including no-break spaces) are word delimiters, so they are not in your word list (MW).
It will work if we use the no-break space as text item delimiter:
use scripting additions
set theResult to {}
set ourText to "Hello my friends Jon Doe, Jane Doe and Joe Doe!" # Each name contains a no-break space
set findThis to character id 160 # Decimal notation of U+00A0 (no-break space)
set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to findThis # The no-break space
set countTextItems to count of text items of ourText
if countTextItems > 1 then
repeat with i from 1 to countTextItems - 1
set end of theResult to word -1 of text item i of ourText & findThis & word 1 of text item (i + 1) of ourText
end repeat
else
set theResult to "[no character id " & id of findThis & " found]"
end if
set AppleScript's text item delimiters to linefeed
display dialog theResult as text
set AppleScript's text item delimiters to saveTID
Input (ourText):
Hello my friends Jon[noBreakSpace]Doe, Jane[noBreakSpace]Doe and Joe[noBreakSpace]Doe!
Output:
Please note that this will fail in cases like
my friends J.[noBreakSpace]Doe
because we are using word inside the repeat loop.
If you often have cases like these then replace word -1 and word 1 with text -1 and text 1. The output then will only contain the two characters around the spaces, but for searching purposes this is still enough.

Does AppleScript have a replace function?

In VBA, its extrememly easy to replace substrings with a different substring, an example being objMsg.Body = Replace(objMsg.Body, "<Month\", Format(dtDate1, "mmmm")) which replaces "<Month>" with the current month in a email body. I've seen questions similar to this before, but they are a couple years old and tend to have insane workarounds that are almost not even worth my time.
No, but you can use this one (source):
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
usage:
replace_chars(message_string, "string_to_be_replaced", "replacement_string")
(btw, you can nowadays also use JavaScript instead of AppleScript, search for "JavaScript for OS X Automation")
One really simple way to do find/replace is to call a shell script and pass text into it as a string:
set inputText to "My name is Fred."
set findText to "Fred"
set replaceText to "Sally"
set newText to do shell script "sed 's|" & quoted form of findText & "|" & quoted form of replaceText & "|g' <<< " & quoted form of inputText

How do I create an AppleScript to loop through a BBEdit document and move a "-" from the end of a number to the beginning?

I have a dirty report file that contains improperly formatted negative numbers. The ultimate goal is to import these to Excel for analysis. I am using BBEdit to clean the report before importing into Excel. I would like to create an apple script to loop through the report and move the "-" from the back of the number to the front.
Dirty Report Example Rows:
B-EXCAL 02 3684 2.0000- 49.02- 108.00- 58.98- 54.6-
B-MISMH 09-3300 33.0000 722.91 1353.00 630.09 46.6
Desired output:
B-EXCAL 02 3684 -2.0000 -49.02 -108.00 -58.98 -54.6
B-MISMH 09-3300 33.0000 722.91 1353.00 630.09 46.6
I have JavaScript and VBScript experience so I imaging the script working something like this psudo script:
Get contents of current window from BBEdit
for each word in contents
if char(len(word)) = "-"
newWord = "-" + rightTrim(word, 1)
replace(word, newWord)
end if
end for
end
This is my first experience with AppleScript and I am at a complete loss.
Thanks for the help/
I'm not sure you need AppleScript. Will BBEdit's find/replace window work for this? Try the following:
Find: ([0-9\.]+)\-
Replace: \-\1
"Grep" and "Wrap around" should be the only things selected below the "Replace" text area. Then click "Replace All."
If you need to do it to a bunch of reports at once, use the same find/replace patterns with multi file search. If I'm not understanding your question correctly, let me know, but I think you can get this done without AppleScript.
Try this. I'm assuming you can get the text from bbedit (or wherever) into applescript as the variable "originalText". Then you'll have to put the "fixedText" back wherever it belongs. NOTE: in my code I assume the "tab" character separates the words/columns in each line of the origText as Michael J. Barber has it currently formatted. If it is another character (like a space character) then you will have to change the word tab in the code to space.
set originalText to "B-EXCAL 02 3684 2.0000- 49.02- 108.00- 58.98- 54.6-
B-MISMH 09-3300 33.0000 722.91 1353.00 630.09 46.6"
set fixedText to fixNegativeSigns(originalText)
on fixNegativeSigns(theText)
set listText to paragraphs of theText
set fixedText to ""
set {tids, text item delimiters} to {text item delimiters, tab}
repeat with i from 1 to count of listText
set listItems to {}
set thisList to text items of (item i of listText)
repeat with j from 1 to count of thisList
set thisItem to item j of thisList
if text -1 of thisItem is "-" then
set thisItem to "-" & text 1 thru -2 of thisItem
end if
set end of listItems to thisItem
end repeat
set fixedText to fixedText & (listItems as text) & character id 10
end repeat
set text item delimiters to tids
return text 1 thru -2 of fixedText
end fixNegativeSigns
NOTE: when testing my code above you should copy/paste it into Applescript Editor. You will have to fix the tab characters in originalText because they do not survive the copy/paste. So remove the spaces between the words/columns and insert a tab. Then the code will work correctly.

Resources