I'm trying to execute the following microscopic AppleScript via KM's "Execute AppleScript" action, but I can't get it to work properly. The script works 100% fine if executed outside of KM as a normal AppleScript, but within KM it does not work as-is.
I tried various ways, I looked at the applescript tag on the form, the KM's help section — all to no avail. I'd really appreciate some help, at this point
Here's the script:
set extract to do shell script ¬
"sed -E 's/.[?][v]=./\1/'<<<" & ¬
(the clipboard as text)'s quoted form
It looks at an URL in the clipboard (sample: https://www.youtube.com/watch?v=RFvNLP8li10&t=14s ) and does a regex on it, storing result in a variable called extract. So the process for this one line script is:
⠀⠀⠀⠀1. Look at value in clipboard (a youtube url)
⠀⠀⠀⠀2. Find a part based on regex via shell script
⠀⠀⠀⠀3. Store what's been found int a variable called extract
Ideally what I'm looking for is a version of the initial AppleScript that would work inside KM's "Execute AppleScript" action. Many thanks in advance for any help or pointers!
Thanks so much for your reply! Yeah, the reason I need it to work as an AppleScript is because it's a part of a slightly larger AppleScript. In that script everything else works, except for this one line that uses shell. Hence I need it to work as a part of an AppleScript to be used as KM's "Execute AppleScript" action.
Strange. I'm not sure why. It definitely always (literally without a fail) works fine on my end if run as a normal AppleScript.
Purely as a side note, that shell command is performed on the clipboard, which has a URL — in which that regex is done. So this one-liner doesn't really have anything substantial "missing". Maybe it doesn't work simply because there's no "right" URL in your clipboard, as a sample you can use any YouTube url, e.g.: https://www.youtube.com/watch?v=RFvNLP8li10&t=14s
Thanks for trying it out. I checked my "one line script" on my end again, and it does work fine. Not sure what more to say on this, it just works every time outside of KM.
As for your suggestion, I tried replacing my initial one-liner "as is" with yours, and it does not work: KM gives an error when trying to execute it. But maybe I'm missing how to apply it fully or something like that.
set extract to do shell script "sed -E 's/.[?][v]=./\\1/'<<<" & "https://www.youtube.com/watch?v=RFvNLP8li10&t=14s"
Doesn't work for me in KM because the output is "". And I had to escape the \ as well.
Which sed have you installed? Because KM does not know your shells $PATH. So homebrew installations aren't available by default. You can change this by setting the variable ENV_PATH within KM to your path.
I don't know which sed I have. Frankly-speaking, I have no idea what a sed is. So I assume whatever comes standard on macOS. I'll try creating a ENV_PATH variable in KM's preferences.
On an unrelated note, I'm guessing my one-line script doesn't work neither for me nor for you because it requires a different syntax to make it work in KM. Hence I asked the question here in the forum. Based on the code in your last post I see exactly the same syntax as I have now, only with a slightly different regex and a hardcoded url instead of reading value in clipboard. So my assumption is that using same syntax as originally is unlikely to work. But I'm sure I'm missing something here, so I do apologize in advance if I'm mixing things up — this all is pretty new for me.
If you don't know what sed is, you probably don't need to bother with ENV_PATH.
Assuming your extract parameter does only contain letters and numbers this should work:
sed -E 's/.*extract=([a-zA-Z0-9]*).*/\\1/'<<<
Instead of your previous one liner.
What this does:
It matches the url until extract= appears. Then it captures every number of characters from a-z, uppercased and numbers from 0 to 9. then discards the rest. Then it outputs just the captured string.
If you need more characters to work, add them within the brackets [].
If you know the size or a size range of extract, replace the * after the brackets [] with {12,15} (for a Range from 12 to 15 characters) or {12} if it's always exactly 12.
This reduces the chance of false positives.
Three steps in the process as I see them (based on how the script works now):
⠀⠀⠀⠀1. Look at value in clipboard (when script runs its already there) — a youtube url (e.g. https://www.youtube.com/watch?v=RFvNLP8li10&t=14s)
⠀⠀⠀⠀2. Find a part based on regex via shell script — take 11 symbols after "?v=" (no quotes) part
⠀⠀⠀⠀3. Store what's been found int a variable — called extract
I initially created a version of this in AppleScript and it does work, but now I need to migrate it over to KM. I thought I could just take my original AppleScript and KM could run it via "Execute AppleScript", but I guess KM doesn't run them the same way.
Thanks so much for the suggestion. Is there any way to look at the value in the clipboard instead of hardcoding a specific url? I gave that url merely as an example to illustrate the point. My initial line looks at the value in the clipboard.
There is no longer a need to use shell scripts to run RegEx with AppleScript.
ASObjC provides a better RegEx engine that is faster and has more features, and is run directly in AppleScript.
RegexAndStuffLib is a script library providing some basic string manipulation commands. There are commands for changing case, splitting and joining strings, various encoding and decoding commands, a range of regular expression commands, and more. Version 1.0.6 supports passing lists of strings to regex change and regex batch commands.