Go Back   The macosxhints Forums > Working with OS X > OS X Developer



Reply
 
Thread Tools Rate Thread Display Modes
Old 08-23-2010, 07:25 AM   #1
renaultssoftware
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!
__________________
Get Icon Creator and support me.
renaultssoftware is offline   Reply With Quote
Old 08-23-2010, 08:29 AM   #2
SirDice
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
SirDice is offline   Reply With Quote
Old 08-23-2010, 08:35 AM   #3
renaultssoftware
MVP
 
Join Date: Dec 2009
Location: Pembroke, Ontario
Posts: 2,051
That would be nice. How do I incorporate Perl into AppleScript/ObjC?
__________________
Get Icon Creator and support me.
renaultssoftware is offline   Reply With Quote
Old 08-23-2010, 08:37 AM   #4
SirDice
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.
SirDice is offline   Reply With Quote
Old 08-23-2010, 08:40 AM   #5
renaultssoftware
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.
__________________
Get Icon Creator and support me.
renaultssoftware is offline   Reply With Quote
Old 08-23-2010, 08:57 AM   #6
renaultssoftware
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
As you can see I still have to write that Perl script.

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
__________________
Get Icon Creator and support me.

Last edited by renaultssoftware; 08-23-2010 at 09:04 AM.
renaultssoftware is offline   Reply With Quote
Old 08-23-2010, 09:38 AM   #7
Craig R. Arko
Site Admin
 
Join Date: Dec 2001
Location: Minneapolis, MN
Posts: 3,967
After you get it working, submit it as a hint.
Craig R. Arko is offline   Reply With Quote
Old 08-23-2010, 10:01 AM   #8
renaultssoftware
MVP
 
Join Date: Dec 2009
Location: Pembroke, Ontario
Posts: 2,051
What if I don't? Would you help?
__________________
Get Icon Creator and support me.
renaultssoftware is offline   Reply With Quote
Old 08-23-2010, 10:04 AM   #9
SirDice
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".
SirDice is offline   Reply With Quote
Old 08-23-2010, 01:45 PM   #10
renaultssoftware
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
__________________
Get Icon Creator and support me.
renaultssoftware is offline   Reply With Quote
Old 08-23-2010, 02:03 PM   #11
renaultssoftware
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';
__________________
Get Icon Creator and support me.
renaultssoftware is offline   Reply With Quote
Old 08-23-2010, 02:20 PM   #12
bramley
MVP
 
Join Date: Apr 2004
Location: Cumbria, UK
Posts: 2,461
Read "man curl"
bramley is offline   Reply With Quote
Old 08-23-2010, 02:21 PM   #13
renaultssoftware
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.
__________________
Get Icon Creator and support me.

Last edited by renaultssoftware; 08-23-2010 at 02:28 PM.
renaultssoftware is offline   Reply With Quote
Old 08-23-2010, 03:12 PM   #14
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,291
Quote:
Originally Posted by renaultssoftware
This always returns a Please enter a valid URL error:
Code:
  $doc = getprint 'http://qgf.in/api.php?url=$ARGV[0]\n';

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
hayne is offline   Reply With Quote
Old 08-23-2010, 03:13 PM   #15
renaultssoftware
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
I ended the string with \n and it doesn't seem to work.
__________________
Get Icon Creator and support me.

Last edited by renaultssoftware; 08-23-2010 at 03:17 PM.
renaultssoftware is offline   Reply With Quote
Old 08-24-2010, 12:25 AM   #16
NaOH
Hall of Famer
 
Join Date: Dec 2007
Posts: 3,747
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)
NaOH is offline   Reply With Quote
Old 08-24-2010, 12:40 AM   #17
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,291
Quote:
Originally Posted by renaultssoftware
I ended the string with \n and it doesn't seem to work.

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 08:23 AM.
hayne is offline   Reply With Quote
Old 08-24-2010, 01:47 AM   #18
SirDice
MVP
 
Join Date: Aug 2009
Posts: 1,119
Quote:
Originally Posted by renaultssoftware
I ended the string with \n and it doesn't seem to work.

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);
}
SirDice is offline   Reply With Quote
Old 08-24-2010, 07:04 AM   #19
renaultssoftware
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!
__________________
Get Icon Creator and support me.
renaultssoftware is offline   Reply With Quote
Old 08-24-2010, 12:54 PM   #20
renaultssoftware
MVP
 
Join Date: Dec 2009
Location: Pembroke, Ontario
Posts: 2,051
Quote:
Originally Posted by Craig R. Arko
After you get it working, submit it as a hint.

Done! Yay.
__________________
Get Icon Creator and support me.
renaultssoftware is offline   Reply With Quote
Reply

Tags
service, url, url shortening

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 12:22 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.