|
|
#1 |
|
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 |
|
|
|
|
|
#2 |
|
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.
|
|
|
|
|
|
#3 |
|
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". |
|
|
|
|
|
#4 | ||||||||||||||||||||||||||||||||||||||||||||||
|
Hall of Famer
Join Date: Apr 2002
Posts: 3,315
|
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.
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. |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
#5 |
|
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 |
|
|
|
|
|
#6 |
|
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
__________________
Philosophy is a battle against the bewitchment of our intelligence by means of language. -LW- |
|
|
|
|
|
#7 | |||||||||||||||||||||||
|
Major Leaguer
Join Date: Mar 2006
Posts: 280
|
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. |
|||||||||||||||||||||||
|
|
|
|
|
#8 | |||||||||||||||||||||||
|
Hall of Famer
Join Date: Apr 2007
Posts: 4,262
|
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- |
|||||||||||||||||||||||
|
|
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|