Go Back   The macosxhints Forums > OS X Help Requests > AppleScript



Reply
 
Thread Tools Rate Thread Display Modes
Old 05-16-2010, 02:02 AM   #1
hisara
Major Leaguer
 
Join Date: Mar 2006
Posts: 280
Safari: Adding bookmarks via script or other program?

Is there any way to add bookmark to Safari Bookmarks using AppleScript, Unix or ASOC or Cocoa snippet in ASOC app?

I plan to add bookmarks when Safari is not running so there is less problems.

I found Property List Suite in the System Events app, but i can't get it to work correctly. Thanks
hisara is offline   Reply With Quote
Old 05-16-2010, 06:34 AM   #2
renaultssoftware
MVP
 
Join Date: Dec 2009
Location: Pembroke, Ontario
Posts: 2,051
Sorry, but AFAIK there isn't an easy way to add bookmarks in Safari. Most people agree that its scriptability is lacking.
__________________
Get Icon Creator and support me.
renaultssoftware is offline   Reply With Quote
Old 05-16-2010, 06:51 AM   #3
baf
MVP
 
Join Date: Jun 2007
Location: Skellefteċ, Sweden
Posts: 1,173
You could try looking into
Library/Safari/Bookmarks.plist in your home directory.
Either with plutil or defaults it should be possible to do something.
__________________
/Bengt-Arne Fjellner IT-Administrator Luleċ university, Sweden.
Some say: "You learn as long as you live".
My way: "You live as long as you learn".
baf is offline   Reply With Quote
Old 05-16-2010, 09:13 PM   #4
Hal Itosis
Hall of Famer
 
Join Date: Apr 2002
Posts: 3,315
Quote:
Originally Posted by renaultssoftware
Sorry, but AFAIK there isn't an easy way to add bookmarks in Safari. Most people agree that its scriptability is lacking.

Well —if you read what post #1 says, i.e.: "when Safari is not running" —then, clearly this topic can't be about Safari's scriptability anyway.


Quote:
Originally Posted by baf
You could try looking into
Library/Safari/Bookmarks.plist in your home directory.
Either with plutil or defaults it should be possible to do something.

Exactly. The plist will need to be tweaked directly, I'd say either with defaults or PlistBuddy.

Last edited by Hal Itosis; 05-16-2010 at 09:15 PM.
Hal Itosis is offline   Reply With Quote
Old 05-16-2010, 10:52 PM   #5
tw
Hall of Famer
 
Join Date: Apr 2007
Posts: 4,262
it doesn't seem too difficult to manage with applescript and System Events. the plist structure is a simple nested set of dictionaries. the only questionable thing is that it needs a uuid (easy enough to generate through the command line, but when Apple uses uuids in this way they are often cross-referenced with other material (caches, internal databases, etc), so simply creating a new entry may not be enough. however, if you want to give it a try, the code would be something like this (this creates a bookmark at the end of the bookmarks menu; if you want something more generalized - e.g. for the bookmarks bar, or for specific folders - its doable, just a bit more wordy):
Code:
set theUUID to do shell script "uuidgen"
set titleString to "This is the title that will appear in the menu"
set theURIstring to "This is the URI to the page in question"
set thePlistFile to property list file "~/Library/Safari/Bookmarks copy.plist"
tell property list item "Children" of contents of thePlistFile
	tell property list item 3
		(* 
			on my machine, the bookmarks menu list is in the 3rd property list item - it may differ from machine to machine, so you 
			check the 'Title' property to make sure it says BookmarksMenu.
			Also, be careful - the 3rd property list item is called 'Item 2'; the plist is 0-based, applescript is 1-based
		*)
		if value of property list item "Title" is not "BookmarksMenu" then return
		tell property list item "Children"
			set newDict to make new property list item with properties {kind:record}
			tell newDict
				set URIDict to make new property list item with properties {kind:record, name:"URIDictionary"}
				tell URIDict
					make new property list item with properties {name:"title", kind:string, value:titleString}
				end tell
				make new property list item with properties {name:"URLString", kind:string, value:theURIstring}
				make new property list item with properties {name:"WebBookmarkType", kind:string, value:"WebBookmarkTypeLeaf"}
				make new property list item with properties {name:"WebBookmarkUUID", kind:string, value:theUUID}
			end tell
		end tell
	end tell
end tell
__________________
Philosophy is a battle against the bewitchment of our intelligence by means of language. -LW-

Last edited by tw; 05-16-2010 at 10:55 PM. Reason: forgot an important 'value of' specifier
tw is offline   Reply With Quote
Old 05-17-2010, 03:49 AM   #6
tw
Hall of Famer
 
Join Date: Apr 2007
Posts: 4,262
it just occurred to me that I could really tighten up that previous code a lot, and I couldn't resist. here's a much cooler version.

Code:
set theUUID to do shell script "uuidgen"
set titleString to "some title"
set theURIstring to "http://somewhere.someplace.somehow.org"
set thePlistFile to property list file "~/Library/Safari/Bookmarks.plist"
set theDictRec to {WebBookmarkUUID:theUUID, URLString:theURIstring, WebBookmarkType:"WebBookmarkTypeLeaf", URIDictionary:{|title|:titleString}}
tell property list item "Children" of contents of thePlistFile
	set theMenuXML to item 1 of (every property list item whose ((property list item "Title")'s value is "BookmarksMenu"))
	tell theMenuXML's property list item "Children"
		set newDict to make new property list item at end of property list items with properties {value:theDictRec}
	end tell
end tell
I should also warn anyone interested to practice on a copy of your bookmarks plist, and keep a backup copy when you're working on the main plist, because if you mess up the xml references System Events has an unfortunate tendency to zero out the file. don't want all your bookmarks to go up in smoke without a backup.
__________________
Philosophy is a battle against the bewitchment of our intelligence by means of language. -LW-
tw is offline   Reply With Quote
Old 05-17-2010, 08:44 AM   #7
hisara
Major Leaguer
 
Join Date: Mar 2006
Posts: 280
Quote:
Originally Posted by tw
it just occurred to me that I could really tighten up that previous code a lot, and I couldn't resist. here's a much cooler version.

Code:
set theUUID to do shell script "uuidgen"
set titleString to "some title"
set theURIstring to "http://somewhere.someplace.somehow.org"
set thePlistFile to property list file "~/Library/Safari/Bookmarks.plist"
set theDictRec to {WebBookmarkUUID:theUUID, URLString:theURIstring, WebBookmarkType:"WebBookmarkTypeLeaf", URIDictionary:{|title|:titleString}}
tell property list item "Children" of contents of thePlistFile
	set theMenuXML to item 1 of (every property list item whose ((property list item "Title")'s value is "BookmarksMenu"))
	tell theMenuXML's property list item "Children"
		set newDict to make new property list item at end of property list items with properties {value:theDictRec}
	end tell
end tell
I should also warn anyone interested to practice on a copy of your bookmarks plist, and keep a backup copy when you're working on the main plist, because if you mess up the xml references System Events has an unfortunate tendency to zero out the file. don't want all your bookmarks to go up in smoke without a backup.

Thanks, your code looks cool.

Actually, i need to create bookmarks to specific folders, which are multiple folders deep. I know how to write preferences to simple plist file which don't have nested folders, but these plutil, defaults and xml are too much for my scripting skills.
hisara is offline   Reply With Quote
Old 05-18-2010, 11:28 AM   #8
tw
Hall of Famer
 
Join Date: Apr 2007
Posts: 4,262
Quote:
Originally Posted by hisara
Thanks, your code looks cool.

Actually, i need to create bookmarks to specific folders, which are multiple folders deep. I know how to write preferences to simple plist file which don't have nested folders, but these plutil, defaults and xml are too much for my scripting skills.

well, it's programming. sometimes you need to expand your horizons...

maybe it would help if you said what your greater goal here is. it's hard to know how to approach things without knowing what it is you're trying to do.
__________________
Philosophy is a battle against the bewitchment of our intelligence by means of language. -LW-
tw 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 03:01 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, 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.