Good morning. I know this is probably very easy but I’m trying to make baby steps into scripting and my amazon books have yet to arrive.
I have a case where I need to know how to tim the contents of the clipboard and then return the results of that clipboard. The clipboard in this case will always be a seven characters long and I just need to automatically strip it down to the first four characters.
Again I apologize for this newbie question. Thank you in advance.
As expected. A perfect reply in under five minutes. thank you very much. And thank you for the link. I was looking for tutorials earlier and was only finding book reviews. Thank you again.
I found all sorts of handy things about clipboard but haven’t been able to find out how I remove everything after a space in the clipboard or after the | (pipeline). I have text that I want to copy to the clipboard that will be random lengths so I have to depend on the space or pipeline to remove everything after.
set theText to the clipboard
set the clipboard to (characters 1 to "|" of theText) as text
By the way thanks for the great link of tutorials (even though this is an old post still helpful) I have been through most of Ben Waldie’s stuff and all of his tutorials a few times on VTC. Still working through AppleScript 1-2-3
This is a great job for Applescript Text Item Delimiters (TIDs).
In the script below I’ve set them to both space and pipe. This works very predictably if there are only spaces or pipes as delimiters in the string.
If there is a combination of space and pipe delimiters it does the same thing - you just have to wrap your head around the fact that there can be more than one delimiter.
Set them to a single item like this:
set AppleScript's text item delimiters to {" "}
OR
set AppleScript's text item delimiters to " "
TIDs by default are set to {“”}
Changing them can cause unexpected things to happen if you don’t change them back - hence the way I’ve restored them in the script.
Since I run most of my scripts from FastScripts I don’t usually bother to change them back unless it will cause an issue in the individual script. I just make sure the entry state of the TIDs is correct for that script.
But. It’s a good habit to restore them.
set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {" ", "|"}}
set the clipboard to "12345 6789"
set _clip1 to text item 1 of (get the clipboard)
set _clip2 to text item 2 of (get the clipboard)
set _clips1 to text items of (get the clipboard)
set the clipboard to "abcde|fghi"
set _clip3 to text item 1 of (get the clipboard)
set _clip4 to text item 2 of (get the clipboard)
set _clips2 to text items of (get the clipboard)
set the clipboard to "Now_is_the_time_for_all_good men_to_come_to_the|aid_of_their_country."
set _clip5 to text item 1 of (get the clipboard)
set _clip6 to text item 2 of (get the clipboard)
set _clip7 to text item 3 of (get the clipboard)
set _clips2 to text items of (get the clipboard)
set AppleScript's text item delimiters to oldTIDS
{_clip1, _clip2, _clips1, _clip3, _clip4, _clips2}
Thank you I will have to mess with this more to get it to work for me. I will end up removing everything after the pipeline since I often get things that will lead with spaces that I guess I will have to keep since I want the info after the spaces but not after the pipeline. I think you have given me the info I need to get this to work. Thanks for the help.
If you’ll provide a sample of what the input looks like and what you want the output to look like we can easily get there. We’re not limited to TIDs. We can use Sed or Awk and regular expressions or fields.
Okay. Let’s start with a very generic vanilla Applescript example.
set _text to " some text | some more text"
set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "|"}
set _scrape to text item 1 of _text
repeat while character 1 of _scrape is in {space, tab}
set _scrape to text 2 thru -1 of _scrape
end repeat
repeat while character -1 of _scrape is in {space, tab}
set _scrape to text 1 thru -2 of _scrape
end repeat
set AppleScript's text item delimiters to oldTIDS
_scrape
--> "some text"
set _text to " some text | some more text"
do shell script "<<<" & quoted form of _text & " sed -En '
# Remove leading space.
s!^[[:blank:]]+!!
# Remove spaces if any, pipe, and all trailing characters.
s![[:blank:]]*\\|.+!!
p
'"
--> "some text"
Now let’s use the Satimage.osax. (My preferred method most of the time.)
set _text to " some text | some more text"
set _scrape to find text "^\\s*(\\S[^\\|]+?)[[:blank:]]*\\|" in _text using "\\1" with regexp and string result
--> "some text"
set theText to " some text | some more text"
tell application "ASObjC Runner"
set theSplits to split string theText with string "|"
set theResult to modify string (item 1 of theSplits) so it is trimmed
end tell
This is great and just what I was looking for thanks for the help and clarification. Sorry for my ignorance but just since this was posted what is the advantage to accomplishing this with other helper extensions or apps like ASObjC Runner and Satimage.osax? My not so educated guess is that it is simpler to write?
Is there any problem with using this as the script and not setting the clipboard back to what it was after pasted. That part confused me a little from the original reply " It’s a good habit to restore them.".
set _text to the clipboard
do shell script "<<<" & quoted form of _text & " sed -En '
# Remove leading space.
s!^[[:blank:]]+!!
# Remove spaces if any, pipe, and all trailing characters.
s![[:blank:]]*\\|.+!!
p
set the clipboard to the result
No practical advantage in this case, really. They just show that there are other tools for doing it that are possibly more suited if the problem turns out to be more complex than you first outlined. Sometimes happens
No, that was in reference to restoring the value of text item delimiters to their default, not the clipboard.
I agree with Shane generally speaking - this is a very simple case - but it still can be solved more easily using ASObjC_Runner or the Satimage.osax.
set _text to " some text | some more text"
set _found to find text "^\\s*(.+?)\\s*\\|" in _text using "\\1" with regexp and string result
Writing a neat one-liner like this always appeals to me, and regex makes it much easier to account for variable whitespace, etc.
The simple fact that you don’t have to fool with quoted form of or the text-length limit of do shell script is a big deal to me.
You can overcome many of these inconveniences by writing some smart handlers though.
set _text to " some text | some more text"
set sedPat to "
s!^[ ]+!!
s![ ]*\\|.*!!
"
sedRepl("-E", sedPat, _text, true)
on sedRepl(_swtchs, sedPat, _text, _altEol)
set _text to quoted form of _text
set _cmd to "sed" & " " & _swtchs & " " & "'" & sedPat & "'" & " <<< " & _text
do shell script _cmd altering line endings _altEol
end sedRepl
Very nice thank you both for the clarification and all the help with this, it will be very helpful in speeding a lot of redundancy. We will use this dozens of times a day!
I’m working on another AppleScript and I have been at this for hours now trying to make heads or tails of this. It is appealing to not having to add any extensions or helper apps to make the scripts work.
Is there a way to use code similar to what you posted
And do something more like this
Example 1 text in clipboard
“{1} My action for the day” With result
“My action for the day”
Example 2 text in clipboard
“my action 2 for the day {some stuff in curly brackets} stuff after the right curly bracket that I don’t want removed” With result
“my action 2 for the day {some stuff in curly brackets} stuff after the right curly bracket that I don’t want removed”
The second example is intentionally the same because there was no {1}, {2}, {3}, {4} or {5} at the start of the clipboard text. Basically I am trying to only keep what comes after the first "} " (right curly bracket and trailing space) if it has a number before it.
If this is too complicated it would be helpful only keep what comes after the first "} " (right curly bracket and trailing space) and not worry about the number before it.
In other words a result like this could work but is not ideal.
Example 2 text in clipboard
“my action 2 for the day {some stuff in curly brackets} stuff after the right curly bracket that I don’t want removed” With result
“my action 2 for the day {some stuff in curly brackets}”
I was finally able to get this to work after realizing I needed to add a delay. The result would was correct in AppleScript but it would not set the clipboard to the result. I am guessing that has something to do with a delay in accessing ASObjC Runner?
set theText to the clipboard
tell application "ASObjC Runner"
set theSplits to split string theText as text with string "}"
set theResult to modify string (item 2 of theSplits) so it is trimmed
end tell
delay 1
set the clipboard to the theResult as text