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»)
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
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.
I have been at this for two days.
So, using Automator and Applescript, I need to scan a volume (or volumes) and get a path to each file, the file name plus extension, assetID (if there is one) and output each part to a comma separated csv file.
So far, I have the Automator actions sorted out and most of the Applescript part but I am at my wits end. The paths and file names work but extracting the assetID (if there is one) is the problem. Not every file has an assetID (and those I am not interested in). The assetID is always a 10 digit number at the end of the file preceded by an underscore("_") like so - afilename_1234567890.ext. As it is now, the script will display the assetID's of the files it processes but as soon as it gets to a file with no id, I see the following error, "The action “Run AppleScript” encountered an error: Can’t get text 1 thru -1 of "".” Something is getting munged somewhere.
Any help would be greatly appreciated.
The script so far...
on run {input, parameters}
-- save delimiters to restore old settings
set savedDelimiters to AppleScript's text item delimiters
-- set delimiters to delimiter to be used
set AppleScript's text item delimiters to "/"
repeat with aPath in input
-- set a variable to contain the "/" (POSIX) version of the files path
set filesPath to POSIX path of aPath
-- get the file name
set fileName to last text item of filesPath
-- get the file name without the extension
set thePrefix to text 1 thru ((offset of "." in fileName) - 1) of fileName
-- get the asset ID, if there is one
set assetID to rightStringFromRight(thePrefix, "_")
display dialog assetID
if (class of assetID) is integer then
-- get the path only
set pathOnly to ((text items 1 thru -2 of (get POSIX path of aPath)) as Unicode text) & "/"
-- output the path only, file name and asset ID to a comma delimited csv file
display dialog assetID
end if
end repeat
-- restore the old delimiter setting
set AppleScript's text item delimiters to savedDelimiters
end run
on rightStringFromRight(str, del)
local str, del, oldTIDs
set oldTIDs to AppleScript's text item delimiters
try
set str to str as string
if str does not contain del then return str
set AppleScript's text item delimiters to del
set str to str's last text item
set AppleScript's text item delimiters to oldTIDs
return str
on error eMsg number eNum
set AppleScript's text item delimiters to oldTIDs
error "Can't rightStringFromRight: " & eMsg number eNum
end try
end rightStringFromRight
on is_number(number_string)
try
set number_string to number_string as number
return true
on error
return false
end try
end is_number
I finally got the script working. A few wrong assumptions corrected and all is well.
I need to write an AppleScript that can change my files names inside folder.Below is my scenario.
Folder1
|
Folder2
|
imageA.png,imageB.png
Now i need to write an AppleScript that can change name of imageA.png to A.png and imageB.png to B.png.
What should be my AppleScript for this? this is not my exact scenario but i am giving an example above so i can implement more from that.
I have tried below script and it is working well but i am getting error when filename is not exist, means it is not ignoring it.
tell application "Finder"
set the name of file "iOs_142:About1.png" to "About1.png"
set the name of file "iOs_142:FAQs.png" to "FAQs1.png"
end tell
If you really insist on doing this with AppleScript, this should give a template to work forward with:
(* Taken from http://applescript.bratis-lover.net/library/string/#replaceString *)
on replaceString(theText, oldString, newString)
local ASTID, theText, oldString, newString, lst
set ASTID to AppleScript's text item delimiters
try
considering case
set AppleScript's text item delimiters to oldString
set lst to every text item of theText
set AppleScript's text item delimiters to newString
set theText to lst as string
end considering
set AppleScript's text item delimiters to ASTID
return theText
on error eMsg number eNum
set AppleScript's text item delimiters to ASTID
error "Can't replaceString: " & eMsg number eNum
end try
end replaceString
tell application "Finder"
set fs to files of folder POSIX file "/your/folder" as list
end tell
repeat with f in fs
set n to replaceString(name of f, "to be replaced", "")
tell application "System Events" to set name of f to n
end repeat