|
|
#1 |
|
MVP
Join Date: Dec 2009
Location: Pembroke, Ontario
Posts: 2,051
|
Getting a URL from a webpage
Hi all.
I'm trying to write a URL-shortening service using AppleScript/Automator. Basically, I'm using http://qgf.in/api.php?url=phishing.net to shorten URLs and replace them right there. When I use that url (with the actual URL at the end), it shows up on a webpage and displays the new, shortened URL. Using AppleScript, I'd like it to get a selected url, open the API, get the text from there, and replace it. Possible or not? If not, I'll use Objective-C to do this, assuming I can find the API to write a service. Thanks! |
|
|
|
|
|
#2 |
|
MVP
Join Date: Aug 2009
Posts: 1,119
|
It'll be a lot easier if you used perl and libwww.
http://search.cpan.org/~gaas/libwww-perl/lwpcook.pod |
|
|
|
|
|
#3 |
|
MVP
Join Date: Dec 2009
Location: Pembroke, Ontario
Posts: 2,051
|
That would be nice. How do I incorporate Perl into AppleScript/ObjC?
|
|
|
|
|
|
#4 |
|
MVP
Join Date: Aug 2009
Posts: 1,119
|
You can execute shell scripts from within Applescript. I never tried it but a perl script isn't all that different.
|
|
|
|
|
|
#5 |
|
MVP
Join Date: Dec 2009
Location: Pembroke, Ontario
Posts: 2,051
|
I know the syntax: do shell script "foo -bar" but I can't figure out how to make a one-liner, nor concatenate the string.
|
|
|
|
|
|
#6 |
|
MVP
Join Date: Dec 2009
Location: Pembroke, Ontario
Posts: 2,051
|
Here's my script so far:
Code:
on run {input, parameters}
set theURLtoShorten to the first item of the input
set shellScript to do shell script "foo -bar http://www.apple.com/" & theURLtoShorten
return shellScript
end run
EDIT: Replacing it with a one-liner Perl gave me this: error "sh: -c: line 0: unexpected EOF while looking for matching `'' sh: -c: line 1: syntax error: unexpected end of file" number 2 Last edited by renaultssoftware; 08-23-2010 at 10:04 AM. |
|
|
|
|
|
#7 |
|
Site Admin
Join Date: Dec 2001
Location: Minneapolis, MN
Posts: 3,988
|
After you get it working, submit it as a hint.
|
|
|
|
|
|
#8 |
|
MVP
Join Date: Dec 2009
Location: Pembroke, Ontario
Posts: 2,051
|
What if I don't? Would you help?
|
|
|
|
|
|
#9 |
|
MVP
Join Date: Aug 2009
Posts: 1,119
|
What little I've read about it, you would save your perl script (make sure the first line is #!/usr/bin/perl) and make it executable. The script should be relatively self contained i.e. accept an argument and return a result. You can call that script with the Applescript do shell script "/some/where/perl.pl".
|
|
|
|
|
|
#10 |
|
MVP
Join Date: Dec 2009
Location: Pembroke, Ontario
Posts: 2,051
|
Yes, I could do that. Only thing is, I don't know how to get arguments and return a result. I know some bash but not much. :-s
|
|
|
|
|
|
#11 |
|
MVP
Join Date: Dec 2009
Location: Pembroke, Ontario
Posts: 2,051
|
This always returns a Please enter a valid URL error:
Code:
#!/usr/bin/perl #---------------------# # PROGRAM: argv.pl # #---------------------# use LWP::Simple; $doc = getprint 'http://qgf.in/api.php?url=$ARGV[0]\n'; |
|
|
|
|
|
#12 |
|
MVP
Join Date: Apr 2004
Location: Cumbria, UK
Posts: 2,461
|
Read "man curl"
|
|
|
|
|
|
#13 |
|
MVP
Join Date: Dec 2009
Location: Pembroke, Ontario
Posts: 2,051
|
Man oh man, I'll have to curl up with it?
I didn't find anything in there.
Last edited by renaultssoftware; 08-23-2010 at 03:28 PM. |
|
|
|
|
|
#14 | |||||||||||||||||||||||
|
Site Admin
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
|
In Perl, like in Bash, putting something inside single quotes means that it will be taken literally - i.e. no variable interpolation. To get variable interpolation (your $ARGV[0]), you want double quotes.
__________________
hayne.net/macosx.html |
|||||||||||||||||||||||
|
|
|
|
|
#15 |
|
MVP
Join Date: Dec 2009
Location: Pembroke, Ontario
Posts: 2,051
|
Doh! Silly me. I should learn Perl sometime soon.
It worked, except that it's putting the return URL and the prompt on the same line, like this: Code:
.MacBookPro:~ andre$ ./GetURL.pl http://www.apple.com http://qgf.in/zdGtC6MacBookPro:~ andre$ nano GetURL.pl Last edited by renaultssoftware; 08-23-2010 at 04:17 PM. |
|
|
|
|
|
#16 |
|
Hall of Famer
Join Date: Dec 2007
Posts: 3,851
|
In case it's of any use, here's the script I use for URL shortening. This is not my work and I can't say at this point where it was acquired.
Code:
tell application "Safari" set longURL to (get URL of front document) end tell do shell script "curl http://bit.ly/api?url=" & longURL set shortURL to the result set the clipboard to (shortURL as text) |
|
|
|
|
|
#17 | |||||||||||||||||||||||
|
Site Admin
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
|
If you are going to ask for help with a programming question, you need to show your code.
__________________
hayne.net/macosx.html Last edited by hayne; 08-24-2010 at 09:23 AM. |
|||||||||||||||||||||||
|
|
|
|
|
#18 | |||||||||||||||||||||||
|
MVP
Join Date: Aug 2009
Posts: 1,119
|
Wrong place ![]() You've added a newline to the argument, not the result. Code:
#!/usr/bin/perl
#
use LWP::Simple;
my $url = shift || usage();
getprint("http://bit.ly/api?url=$url");
print "\n";
sub usage {
print "Usage:\n";
print "GetURL <url>\n";
exit(1);
}
|
|||||||||||||||||||||||
|
|
|
|
|
#19 |
|
MVP
Join Date: Dec 2009
Location: Pembroke, Ontario
Posts: 2,051
|
See? I really need to learn Perl, don't I?
Either way, it's working!
|
|
|
|
|
|
#20 | |||||||||||||||||||||||
|
MVP
Join Date: Dec 2009
Location: Pembroke, Ontario
Posts: 2,051
|
Done! Yay. |
|||||||||||||||||||||||
|
|
|
![]() |
| Tags |
| service, url, url shortening |
|
|