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



Reply
 
Thread Tools Rate Thread Display Modes
Old 05-20-2007, 06:32 PM   #1
glitch1138
Prospect
 
Join Date: Jul 2006
Posts: 22
applescript help? need to pull url from a txt file and open it in a firefox window?

basically i have text file that contains a URL as:

[InternetShortcut]
URL=http://forums.macrumors.com/

how do i get applescript to pull out the "http://forums.macrumors.com/" and send it to Firefox?

thanks for help. i'm very new to applescript...
glitch1138 is offline   Reply With Quote
Old 05-20-2007, 07:13 PM   #2
tw
Hall of Famer
 
Join Date: Apr 2007
Posts: 4,262
is this a text file, or a text clipping?
if it's a text file, what application opens it (TextEdit, Word, TextWrangler, etc...)?
is the file open when you want to run the applescript, or do you want the applescript to get the URL without opening the application?
tw is offline   Reply With Quote
Old 05-20-2007, 07:35 PM   #3
glitch1138
Prospect
 
Join Date: Jul 2006
Posts: 22
Quote:
Originally Posted by tw
is this a text file, or a text clipping?
if it's a text file, what application opens it (TextEdit, Word, TextWrangler, etc...)?
is the file open when you want to run the applescript, or do you want the applescript to get the URL without opening the application?

it's technically a .URL file from a windows XP system, which Safari can open but when I set firefox to open it Firefox chokes on it.

i figured since .URL files are just text files with the URL link, I would create an applescript that would pull the url from the file and pass it on to firefox.

i don't know if this is feasible, but it would ease my transition from PC to Apple (I have hundreds of URL links scattered across my media drive that I would prefer to open in Firefox.)

thanks for any help you can provide...
glitch1138 is offline   Reply With Quote
Old 05-20-2007, 08:11 PM   #4
tw
Hall of Famer
 
Join Date: Apr 2007
Posts: 4,262
well, if it's just a text file, and the contents are in this form - URL=http://forums.macrumors.com - and nothing else, this should work:
Code:
set filePath to <path to .url file>
     -- Apple paths are colon delimited, eg. HD name:Users:Desktop:whatever.url

     -- these lines open the .url file, extract the text and close it again
set fp to open for access filePath
set fileContents to read fp
close access fp

     -- this extracts just the URL portion (text items will split the text at '='
set tid to applescript's text item delimiters
set applescript's text item delimiters to "="
set URLstring to last text item of fileContents
set applescript's text item delimiters to tid

     -- load it in firefox
tell application "Firefox"
	activate
	OpenURL URLstring
end tell
note that this might bust if you have URLs with get statements (which would probably look like URL=http://forums.macrumors.com?something=yes), since the second equals would throw things off. also, this only does one; assuming this works, and given a little creativity, you could write a script that would open all of your .URLs and import them into bookmarks, or even Mac style webclippings.

I can't test this, unfortunately, because I don't have a .URL file. if you want to post one so I can see it, I could say more.

Last edited by tw; 05-20-2007 at 08:15 PM.
tw is offline   Reply With Quote
Old 05-20-2007, 11:11 PM   #5
glitch1138
Prospect
 
Join Date: Jul 2006
Posts: 22
Thanks, TW. I greatly appreciate your help.

I had difficulty figuring out the path stuff (new to applescript). Eventually I used an app that copies the unix style path to the clipboard and used the POSIX command (found online). when it ran it opened firefox and went to www.19,2.com

i think this is because there's a 2nd "=" in my url and it chokes on it. after i ran the script it changed the path line itself and now it gives out "Can't make file "Macintosh HD:Users:writer:Desktop:test.url" into type reference."

i suppose the best way to post a URL file is to post the text of it and it could be changed from a .TXT to a .URL. my test.url is:

[DEFAULT]
BASEURL=http://forums.macrumors.com/
[InternetShortcut]
URL=http://forums.macrumors.com/
IDList=
IconFile=http://forums.macrumors.com/favicon.ico
IconIndex=1
[{000214A0-0000-0000-C000-000000000046}]
Prop3=19,2

your script that i'm having trouble with the path is currently:


set filePath to file "Macintosh HD:Users:writer:Desktop:test.url"

-- Apple paths are colon delimited, eg. HD name:Users:Desktop:whatever.url
-- these lines open the .url file, extract the text and close it again

set fp to open for access filePath
set fileContents to read fp
close fp

-- this extracts just the URL portion (text items will split the text at '='
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "="
set URLstring to last text item of fileContents
set AppleScript's text item delimiters to tid

-- load it in firefox
tell application "Firefox"
activate
OpenURL URLstring
end tell




thanks again for helping...
glitch1138 is offline   Reply With Quote
Old 05-20-2007, 11:47 PM   #6
tw
Hall of Famer
 
Join Date: Apr 2007
Posts: 4,262
ok, some progress anyway...

the path problem, I'm think, comes from something I left out of the original post (I edited it later, but you must have gotten the first draft). you have to say close access fp, not just close fp. my bad...

now, because the text in the file is more complex than I originally though, you'll have to change the rest a bit. try this:
Code:
set filePath to file "Macintosh HD:Users:writer:Desktop:test.url"

	-- these lines open the .url file, extract the text and close it again
set fp to open for access filePath
set fileContents to read fp
close access fp

	-- a paragraph in applescript is a segment of text followed by a 
	-- line feed or return character. this should extract the second line 
	-- (begins BASEURL).  you might try paragraph 4 instead (begins URL);
	-- I don't know if there's a difference.
set secondLine to paragraph 2 of fileContents

	-- this extracts just the URL portion (text items will split the text at '='
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "="
set URLstring to last text item of secondLine
set AppleScript's text item delimiters to tid

	-- load it in firefox
tell application "Firefox"
	activate
	OpenURL URLstring
end tell
I'm assuming, of course, that all .URL files have a common and consistent structure. you might check a few to be sure...
tw is offline   Reply With Quote
Old 05-21-2007, 01:24 AM   #7
glitch1138
Prospect
 
Join Date: Jul 2006
Posts: 22
hey (after 1 small change) it worked!
i had the to remove the word "file" from the first line so it was:
Code:
set filePath to "Macintosh HD:Users:writer:Desktop:test.url"
After that it stopped choking on it. Next I added an "open handler" (learning this as we go) and saved it as an application, which (i guess) makes it a droplet now because I'm able to drag a URL file to it and it works.
I guess as long as the URL files I have are the same standard then it'll continue to work.

my droplet code below:

Code:
on open the_droppings
	set filePath to the_droppings
	
	
	-- these lines open the .url file, extract the text and close it again
	set fp to open for access filePath
	set fileContents to read fp
	close access fp
	
	-- a paragraph in applescript is a segment of text followed by a 
	-- line feed or return character. this should extract the second line 
	-- (begins BASEURL).  you might try paragraph 4 instead (begins URL);
	-- I don't know if there's a difference.
	set secondLine to paragraph 2 of fileContents
	
	-- this extracts just the URL portion (text items will split the text at '='
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "="
	set URLstring to last text item of secondLine
	set AppleScript's text item delimiters to tid
	
	-- load it in firefox
	tell application "Firefox"
		activate
		OpenURL URLstring
	end tell
end open
Thank you TW. you have no idea how much this means to me and how much it will make my work easier.
If you have any code improvements/ streamlining, I'll certainly take them.

Much appreciated...
glitch1138 is offline   Reply With Quote
Old 03-04-2008, 08:22 PM   #8
mamawildbear
Guest
 
Posts: n/a
Update to Code - add notification

This thread is an older one but as a recent mac convert, I found it useful. If you add a bit of additional code to the beginning, you can add a pop-up with instructions for people who mistakenly launch the app instead of dragging a file onto it. (Or if you're a dork who forgot how to use your own file - like me!)


Code:
on run
	display dialog "Drag and drop an Internet Explorer .url file onto this application to open in Firefox" buttons ("Ok") default button 
		"Ok" -- this is a drag drop, not run
end run

on open the_droppings
	set filePath to the_droppings
	
	
	-- these lines open the .url file, extract the text and close it again
	set fp to open for access filePath
	set fileContents to read fp
	close access fp
	
	-- a paragraph in applescript is a segment of text followed by a 
	-- line feed or return character. this should extract the second line 
	-- (begins BASEURL).  you might try paragraph 4 instead (begins URL);
	-- I don't know if there's a difference.
	set secondLine to paragraph 2 of fileContents
	
	-- this extracts just the URL portion (text items will split the text at '='
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "="
	set URLstring to last text item of secondLine
	set AppleScript's text item delimiters to tid
	
	-- load it in firefox
	tell application "Firefox"
		activate
		OpenURL URLstring
	end tell
end open
  Reply With Quote
Old 02-23-2012, 01:02 PM   #9
Rumboogy
Prospect
 
Join Date: Feb 2012
Posts: 2
Syntax error

When I try this script I get the following error:
Syntax Error
Expected end of line, etc. but found identifier.
And the "URLstring" portion of the "OpenURL URLstring" line is selected. Does anyone know how to solve this?
Rumboogy is offline   Reply With Quote
Reply

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 10:38 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.