Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nicolaselhani

macrumors member
Original poster
Oct 1, 2015
90
49
Hello,

I'm new to AppleScript

Trying to write a script that will:

1. Select and copy the current Safari URL
2. Send it to bit.ly for URL Shortening
3. Copy the result to the clipboard

I've come up with this so far:

property theURL : ""
property bitlyToken : "0c60279f1425ac413d021f797d8c96f7dc5834af"

tell application "Safari"
set theURL to URL of current tab of window 1
end tell


set
shellScript to ("curl --url \"https://api-ssl.bitly.com/v4/shorten\" ¬
--data access_token=" & bitlyToken & "&format=txt&longUrl=" & theURL)

set the clipboard to shellScript

Unfortunately the result I'm getting on the clipboard is this whole line:

curl --url "https://api-ssl.bitly.com/v4/shorten" ¬
--data access_token=0c60279f1425ac413d021f797d8c96f7dc5834af&format=txt&longUrl=https://www.bbc.com/


instead of just the bit.ly of https://www.bbc.com

If anyone has any ideas of what I'm missing, would be really appreciated,

Thanks

Nick
 
You are just setting a string and copying it, not executing it and getting a result (I am guessing you are wanting to use it for do shell script).
 
Thanks for your reply @Red Menace

You're right, yeah I'm still new to coding and AppleScript but thought this would be a fun project to start with. So once I add do shell script ShellScript I still am getting the full string of code as described in my original post. Is there something wrong with the code formatting or might it have to do with the bitly api structure/variables?

Thanks again
 
Are you setting the clipboard to the results of do shell script?
The script works (although I don't have a valid token), you just need to do something like:

AppleScript:
--
set shellScript to "curl --url \"https://api-ssl.bitly.com/v4/shorten\" --data access_token=" & bitlyToken & "&format=txt&longUrl=" & theURL
set the clipboard to (do shell script shellScript)
 
Thanks again for your reply and help. Yes I set the results of do shell script to the clipboard, but your way is definitely more efficient and cleaner. In the code below I have included my token, you can use it to test it out it's no problem. I'm getting an error


property theURL : ""


property bitlyToken : "0c60279f1425ac413d021f797d8c96f7dc5834af"





tell application "Safari"


set theURL to URL of current tab of window 1


end tell





set
shellScript to ("curl --url https://api-ssl.bitly.com/v4/shorten?&access_token=" & bitlyToken & "&format=txt&longUrl=" & theURL)





set the clipboard to (do shell script shellScript)





The error reads as
error "sh: value: command not found

% Total % Received % Xferd Average Speed Time Time Time Current

Dload Upload Total Spent Left Speed



0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0

0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0

100 19 100 19 0 0 62 0 --:--:-- --:--:-- --:--:-- 65" number 127

Do you think this is a returned value error from bitly? which would be progress since theURL is actually being sent there.

Thanks again for your help, it's really appreciated
 
I am getting "FORBIDDEN" or "Method Not Allowed" error messages, but yours is showing an error in the script (the sh: value: command not found is coming from the shell). Make sure the shell script you are using is properly formed.
 
Referring to this code:
Code:
 set shellScript to ("curl --url https://api-ssl.bitly.com/v4/shorten?&access_token=" & bitlyToken & "&format=txt&longUrl=" & theURL)
There are multiple &'s in the URL string. As given, the shell will treat them as "run in background" delimiters. You need to escape each & with a backslash, or use quotes around the entire string, so the shell treats it as a single string instead of as a series of background commands.

The &'s seen by the shell are before the words: access_token, format, and longUrl.

I suggest logging the value of the AppleScript variable shellScript, so you can see exactly what's in it. If you do that, please post the logged output.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.