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
Related
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.
This script is for use in Capture One where I am assigning people's names to the EXIF data.
I am trying to return the results of a list that could be one or more choices made by the user. I can get it to work with using item 1 in the list but I can't figure out how to deal with someone choosing 2 or more names from anywhere in the list?
Thanks for any help you can offer.
tell application "Capture One 11"
set peopleChoices to {"Abbie", "Charlie", "De-Arne", "Dean", "Jason", "Marlene", "Peta ", "Phoenix", "Rod", "Vanessa", "Yvonne"}
set peopleList to choose from list peopleChoices with prompt "Select your keyword/s:" with multiple selections allowed
if the result is not false then
set exif_keywords to item 1 of the result
end if
set selectedVariants to get selected variants
repeat with i from 1 to number of items in selectedVariants
set this_item to item i of selectedVariants
set theID to id of (parent image of this_item)
do shell script "/usr/local/bin/exiftool -Subject='" & exif_keywords & "' -m -overwrite_original_in_place " & quoted form of theID
reload metadata this_item
end repeat
display dialog "EXIF data has been updated"
end tell
You are constraining the list to one item in this line
set exif_keywords to item 1 of the result
Just change it to
set exif_keywords to result
I don't know how the keywords are supposed to be passed in the exiftool line, you might flatten the list with text item delimiters, this example joins the list comma separated. Replace "," with space if the parameters have to be space separated.
set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","}
set exif_keywords to exif_keywords as text
set AppleScript's text item delimiters to ASTID
I have included the whole working script below in case anyone else is looking for something similar. The "-sep" is part of exiftool and splits the string depending on what you put after it. I had to escape it for the shell script line but it nromally does not have the back slashes.
tell application "Capture One 11"
set peopleChoices to {"Abbie", "Charlie", "De-Arne", "Dean", "Jason", "Marlene", "Peta", "Phoenix", "Rod", "Vanessa", "Yvonne"}
set peopleList to choose from list peopleChoices with prompt "Select your keyword:" with multiple selections allowed
if the result is not false then
set exif_keywords to result
set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","}
set exif_keywords to exif_keywords as text
set AppleScript's text item delimiters to TID
end if
set selectedVariants to get selected variants
repeat with i from 1 to number of items in selectedVariants
set this_item to item i of selectedVariants
set theID to id of (parent image of this_item)
do shell script "/usr/local/bin/exiftool -sep \",\" -Keywords='" & exif_keywords & "' -m -overwrite_original_in_place " & quoted form of theID
reload metadata this_item
end repeat
display dialog "EXIF data has been updated"
end tell
What i want is to do a find and replace command with applescript. I know I can use namechanger and other programs but that takes too many steps. I'm all about efficiency.
tell application "Finder"
set selected_items to selection
set jobNum to text returned of (display dialog "Job Number:" default answer "")
set name of document folder selection to jobNum (*if it did work it would rename the entire folder which isn't what I want. My goal is to replace all "12345" with the value of jobNum*)
end tell
sample folder structure (i can't submit images yet because I need 10pts)
main folder: 12345_Sample Job Folder
- 12345_D.ai
- 12345_P.ai
- Proofs
- Working Files
- 12345.ai
Try this script:
set jobFolder to choose folder with prompt "Choose the Job Folder:"
tell application "Finder"
set rootName to jobFolder's name
set searchString to my FindSearchString(rootName)
set replaceString to text returned of (display dialog ("This script will search for files that contain " & searchString & ". Please enter the replacement text and click OK.") default answer searchString)
set everything to every file in (jobFolder's entire contents)
repeat with onething in everything
if ((onething's name) as text) contains searchString then
set onething's name to my replace_chars(((onething's name) as text), searchString, replaceString)
end if
end repeat
set jobFolder's name to my replace_chars(((jobFolder's name) as text), searchString, replaceString)
end tell
---------------------------------
on replace_chars(this_text, search_string, replacement_string)
set astid to AppleScript's text item delimiters
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 astid
return this_text
end replace_chars
---------------------------------
to FindSearchString(txt)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "_"
set sst to txt's text item 1
set AppleScript's text item delimiters to astid
return sst
end FindSearchString
This will only affect filenames within the initial chosen folder, not subfolder names. At the very end, it changes the name of the chosen folder.
The renaming handler is based on the code at the bottom of this page
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*)
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