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.
Related
I have to adjust a lot of files to remove the last part of them:
From this:
108595-1121_gemd_u65_stpetenowopen_em_f_2021-12-03T161809.511773.zip
To this:
108595-1121_gemd_u65_stpetenowopen_em_f.zip
It's always 24 characters that need to be stripped and there is always an underscore at the beginning. The rest is random numbers and characters. I found code below to remove numbers, but I need characters.
My goal is to put this in an automator script with some other processes, but the Renamer in Automator isn't robust enough.
How can I have it strip X-number of characters?
on run {input, parameters}
repeat with thisFile in input
tell application "Finder"
set {theName, theExtension} to {name, name extension} of thisFile
if theExtension is in {missing value, ""} then
set theExtension to ""
else
set theExtension to "." & theExtension
end if
set theName to text 1 thru -((count theExtension) + 1) of theName -- the name part
set theName to (do shell script "echo " & quoted form of theName & " | sed 's/[0-9]*$//'") -- strip trailing numbers
set name of thisFile to theName & theExtension
end tell
end repeat
return input
end run
No need to use do shell script here, which just confuses the issue. Since your names are underscore-delimited, just use AppleScript's text item delimiters:
repeat with thisFile in input
tell application "Finder"
set {theName, theExtension} to {name, name extension} of thisFile
set tid to my text item delimiters
set my text item delimiters to "_"
set nameParts to text items of theName
set revisedNameParts to items 1 through -2 of nameParts
set newName to revisedNameParts as text
set my text item delimiters to tid
if theExtension is not in {missing value, ""} then
set newName to newName & "." & theExtension
end if
set name of thisFile to newName
end tell
end repeat
return input
What this does, in words:
lines 4 and 5 first save the current text item delimiter (TID) value and then set it to '_'
line 6 breaks the name-string into a list of string parts by cutting the name string at the character '_'
line 7 drops the last item in that list (which is everything after the last '_')
line 8 reverses the process, combining the shortened list of text items into a single string by joining them with '_'
The remainder resets the TID value to its original state, adds the extension to the string, and changes the file name
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
I've got a simple code reading a text file into a list. It's a list of CMYK values in this format: 00, 100, 64, 33. For some reason, the output is replacing spaces with strange characters... " " (return and dagger?).
So this script:
set cmykList to {}
set eachLine to paragraphs of (read POSIX file "/Users/me/Desktop/cmyk.txt")
repeat with nextLine in eachLine
if length of nextLine is greater than 0 then
copy (nextLine as text) to the end of cmykList
end if
end repeat
choose from list cmykList
returns:
00, 100, 64, 33,
00, 00, 00, 00,
100, 72, 00,
100, 35, 00, 100
Any ideas on why this is, and how I can avoid this?
The text file is set up like so:
00, 100, 64, 33
00, 00, 00, 00
100, 72, 00, 18
100, 35, 00, 100
00, 16, 100, 00
00, 100, 63, 29
00, 66, 100, 07
03, 00, 00, 32
100, 35, 00, 100
00, 100, 81, 04
04, 02, 00, 45
00, 00, 00, 00
03, 00, 00, 32
100, 35, 00, 100
Edit: Resolved this issue doing a find / replace:
set cmykList to {}
set eachLine to paragraphs of (read POSIX file "/Users/me/Desktop/cmyk.txt")
repeat with nextLine in eachLine
if length of nextLine is greater than 0 then
set theText to (nextLine as text)
set AppleScript's text item delimiters to " "
set theTextItems to text items of theText
set AppleScript's text item delimiters to " "
set theText to theTextItems as string
set AppleScript's text item delimiters to {""}
copy (theText as text) to the end of cmykList
end if
end repeat
set chooseList to choose from list cmykList
But still very curious as to why this happened in the first place.
Those two characters (ASCII 194 160) are the UTF-8 representation of a Unicode NO-BREAK SPACE character.
You don't specify the source of your text file but wherever it's coming from is using non-breaking spaces instead of regular spaces. As you found out, you can fix the issue by replacing them with regular spaces when you read the file in.
Your file contains Unicode text in UTF8 encoding. Standard Additions’s read and write commands (stupidly) use ancient classic-MacOS-era legacy encodings by default, so you need to tell them to use UTF8 explicitly:
set eachLine to paragraphs of (read POSIX file "/Users/me/Desktop/cmyk.txt" as «class utf8»)
Is there a way, say using a space char as the string delimiter, set a string to the first three words of a paragraph including spaces.
For example
set a to "This is my test string"
set b to words 1 thru to 3 of a
set c to words 1 thru to 3 of a as rich text
return {b,c}
Returns {{"This","is","my"},"Thisismy"}
I want to set a variable so in this case of a, it would be set to "This is my".
First let's explain what happens. words 1 thru 3 of a as rich text is getting a range of words as an list. Then as rich text (which should be as string) coerces the list into an string. When you coerce an list (or record) to an string AppleScript will use an separator called text item delimiter. By default it is set to "". This means there is no separator (delimiter) used and the words are glued together. But let's see what happens when we set temporarily the text item delimiters to space.
set a to "This is my test string"
set b to words 1 thru 3 of a
set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
set c to words 1 thru 3 of a as string
set AppleScript's text item delimiters to oldTID
return {b, c}
now it returns {{"This", "is", "my"}, "This is my"}
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