export safari bookmark folder into html

i am a newbie and would like to create an apple script that will accomplish the following:

take any bookmarks saved within a specific bookmark folder and export them into a text file where the actual website that is bookmarked is turned into html within the said text document.

for example:
bookmark1 at address http://www.bookmark1.com becomes:
a href=“http://www.bookmark1.com”>bookmark1< /a — i have omitted the neccassary opening and closing brackets of the html so that the actual code can be seen in this post—

bookmark2 would follow on the next line [following a return] and this would continue till all the bookmarks in this bookmark folder were output to html all contained within a single text document.

is this possible? any direction or help would be very much appreciated.

Check out this script from Fabrizio Deledda in our ScriptBuilders section. It’s not exactly what you are looking for, but it may give you some hints…

http://scriptbuilders.net/category.php?search=safari+bookmark

thanks for the tip… this script is somewhat similar to what i am looking to do… however since the script is run-only it gives me little clues as to how to achieve what i am looking to do. i have contacted the author… perhaps he will be gracious enough to share his knowledge.

best,
yaj

Here’s a script from T.J. Mahaffey that exracts the bookmarked URLs from Safari and puts them in a TextEdit doc.

http://scriptbuilders.net

[This script was automatically tagged for color coded syntax by Script to Markup Code]

It took about two minutes to extract 850 URLs on my machine.

greg -

thanks so much for posting this… it is very close to what i am looking for.

it is missing two elements listed in order of importance:

[1] the bookmark description inside of an tag. for example i would love for the export of the urls to be presented as such:

a href="http://www.macscripter.net>greatest apple script site in all of the land< a>
+++ the above example is broken html for demonstration purposes only.

[2] the ability to extract specific folders or maintain the folders in the text document

took 20 seconds for me to export 516 urls.

I am hitting the drawing board n this one… see if i can come up with anything.

thanks again for the help,
jay

Jay, if you change the tenth line

set fURL to ("http://" & newURL)

in the above script to this…

set fURL to ("<a href="http://" & newURL & "">")

It will encase the URLs in html tags for you. I’ll leave getting the title as an exercise for somone else ;¬)

I was looking for something similar, but only bookmarks in a certain folder. Can’t make any sense of the XML files folder structure.

I came up with the following script. It parses out the XML file and keeps the directory structure then generates a javascript text file that I used to make my “blogroll” links on my website (check out the right column on my website for an example). However it’s not perfect, it doesn’t seem to get the links not in a folder and it doesn’t work with Tiger. I’d appreciate any input on this.

display dialog "Please enter the root directory (SafariBookmarsk2Web Beta 1)" default answer "BookmarksBar"

(*buttons {"Next", "Finish"}


set whichButton to (button returned of result)
if whichButton is "Next" then
	
end if
*)
set RootDirectory to (text returned of result) --set RootDirectory to "BookmarksBar"
set TheDate to (current date)



set the_list to paragraphs of (read file (((path to current user folder) as string) & "Library:Safari:Bookmarks.plist"))

set DirectoryLevel to 0
set DirectoryNumber to 0
set URLCount to 0
set MasterURLDirectory to {}
set SubURLitems to {}
set MasterDirDirectory to {}
set SubDirItems to {}
set CurrentDirectory to ""
--Pre clean list
log "count of the_list: " & (count of the_list) as string
(*repeat with n from 1 to (count of the_list)
	if item n of the_list contains tab then set (item n of the_list) to searchReplace((item n of the_list), tab, "")
end repeat *)
--Parse List
set n to 1
repeat (count of the_list) times
	
	set n to n + 1
	--log "item n of the_list: " & (item n of the_list)
	if item n of the_list contains "</plist>" then
		exit repeat
	else if item n of the_list contains "<dict>" and item (n + 1) of the_list contains "<key>Children</key>" then
		set n to n + 1
		set DirectoryLevel to DirectoryLevel + 1
	else if item n of the_list contains "</array>" then
		set n to n + 1
		set FolderTitle to searchReplace(searchReplace(searchReplace((item (n + 1) of the_list), "<string>", ""), "</string>", ""), tab, "")
		if FolderTitle is "<integer>1</integer>" then set FolderTitle to "Root"
		set FolderTitle to searchReplace(FolderTitle, "'", "")
		if CurrentDirectory is "" then
			set CurrentDirectory to FolderTitle
		else
			set CurrentDirectory to CurrentDirectory & ":" & FolderTitle
		end if
		if DirectoryLevel is greater than 0 then set DirectoryLevel to DirectoryLevel - 1
		set DirectoryNumber to DirectoryNumber + 1
		set n to n + 6
		
		copy FolderTitle to end of SubDirItems
		copy DirectoryLevel to end of SubDirItems
		copy DirectoryNumber to end of SubDirItems
		copy SubDirItems to the beginning of MasterDirDirectory
		set SubDirItems to {}
		
		--log "n: " & n
		--log "FolderTitle: " & FolderTitle
		--log "DirectoryLevel: " & (DirectoryLevel as string) & " Directory Name: " & FolderTitle
		--log (round ((n / (count of the_list)) * 100))
	else if item n of the_list contains "<dict>" and item (n + 1) of the_list contains "<key>URIDictionary</key>" then
		set n to n + 1
		set theAddress to searchReplace(searchReplace((item (n + 3) of the_list), "<string>", ""), "</string>", "")
		--Find title of theAddress
		repeat with o from 1 to 7
			if item (n + o) of the_list contains "Title" then
				set theTitle to searchReplace(searchReplace((item (n + o + 1) of the_list), "<string>", ""), "</string>", "")
				exit repeat
			end if
		end repeat
		set n to n + o + 7
		--log "n: " & n
		
		if theTitle contains "||" then
			set SplitAddress to {}
			set OldDelims to AppleScript's text item delimiters
			set AppleScript's text item delimiters to "||"
			set SplitAddress to text items of theText
			set AppleScript's text item delimiters to OldDelims
			set CurrentURL to searchReplace(("<a href=\"" & theAddress & "\">" & (item 1 of SplitAddress) & "</a> - " & (item 2 of SplitAddress)), tab, "")
			
		else
			set CurrentURL to searchReplace(("<a href=\"" & theAddress & "\">" & theTitle & "</a>"), tab, "")
		end if
		set CurrentURL to searchReplace(CurrentURL, "'", "")
		copy CurrentURL to end of SubURLitems
		copy DirectoryNumber + 1 to end of SubURLitems
		copy SubURLitems to end of MasterURLDirectory
		set SubURLitems to {}
		set URLCount to URLCount + 1
		--log "URLCount: " & URLCount
	end if
end repeat

set TempList to {}
set littlelist to {}
set NewMasterDirDirectory to {}
repeat with n from 1 to (count of MasterDirDirectory)
	set CurrentCount to (item 2 of (item n of MasterDirDirectory)) + 1
	if (count of TempList) is less than CurrentCount then
		copy item 1 of (item n of MasterDirDirectory) to end of TempList
	else
		if (count of TempList) is greater than item 2 of (item n of MasterDirDirectory) then
			repeat with o from CurrentCount + 1 to (count of TempList)
				set item o of TempList to ""
			end repeat
		end if
		set item ((item 2 of (item n of MasterDirDirectory)) + 1) of TempList to item 1 of (item n of MasterDirDirectory)
	end if
	--log TempList
	set TheString to ""
	repeat with p from 1 to (count of TempList)
		if item p of TempList is not "" then set TheString to TheString & ":" & item p of TempList
	end repeat
	copy TheString to end of littlelist
	copy item 3 of (item n of MasterDirDirectory) to end of littlelist
	copy littlelist to end of NewMasterDirDirectory
	set littlelist to {}
end repeat

--MasterDirDirectory
--NewMasterDirDirectory
set FinalList to {}
set NewURLDir to {}
repeat with x from 1 to (count of MasterURLDirectory)
	--log "URL: " & item 1 of (item x of MasterURLDirectory)
	set TargetDir to item 2 of (item x of MasterURLDirectory)
	--log "TargetDir: " & TargetDir
	set TheDir to "N/A"
	repeat with y from 1 to (count of NewMasterDirDirectory)
		--log "item 2 of (item y of NewMasterDirDirectory): " & item 2 of (item y of NewMasterDirDirectory)
		if item 2 of (item y of NewMasterDirDirectory) is TargetDir then
			set TheDir to item 1 of (item y of NewMasterDirDirectory)
			--log "TheDir (2): " & TheDir
			exit repeat
		end if
	end repeat
	
	--log "TheDir (3): " & TheDir
	--if TheDir is "" then set TheDir to "N/A"
	if TheDir contains RootDirectory then
		--set item 2 of (item x of MasterURLDirectory) to TheDir
		copy (item 1 of (item x of MasterURLDirectory)) to end of FinalList
		--remove extra stuff from start of directory name
		set OldDelims to AppleScript's text item delimiters
		set AppleScript's text item delimiters to ":"
		set DirList to text items of TheDir
		set AppleScript's text item delimiters to OldDelims
		set NewDirName to ""
		set CopyNames to false
		repeat with q from 1 to count of DirList
			if item q of DirList is RootDirectory then set CopyNames to true
			if CopyNames is true and item q of DirList is not RootDirectory and NewDirName is "" then
				set NewDirName to (item q of DirList)
			else if CopyNames is true and item q of DirList is not RootDirectory and NewDirName is not "" then
				set NewDirName to NewDirName & ":" & (item q of DirList)
			end if
		end repeat
		copy NewDirName to end of FinalList
	end if
	
end repeat

--Generate text file contents
set theFileContents to "var urllist = new Array("

--Make links only list
set urlonlylist to {}
repeat with a from 1 to count of FinalList
	if a + 1 is (count of FinalList) and item a of FinalList contains "href" then set theFileContents to theFileContents & "'" & (item a of FinalList) & "'"
	if a + 1 is not (count of FinalList) and item a of FinalList contains "href" then set theFileContents to theFileContents & "'" & (item a of FinalList) & "',"
end repeat
set theFileContents to theFileContents & ");" & return & return
set theFileContents to theFileContents & "urls = urllist.length" & return
set theFileContents to theFileContents & "function randomlink() {" & return
set theFileContents to theFileContents & "randomNum = Math.floor" & return
set theFileContents to theFileContents & "((Math.random() * urls))" & return
set theFileContents to theFileContents & "document.write('<p>Random link: ' + urllist[randomNum] + '</p>')" & return
set theFileContents to theFileContents & "}" & return

set TempText to ""
set CurrDir to ""
repeat with a from 1 to count of FinalList by 2
	if item (a + 1) of FinalList is not CurrDir then
		set CurrDir to (item (a + 1) of FinalList)
		if a is not 1 then set TempText to TempText & "    document.write('</ul></p>');" & return
		set TempText to TempText & "    document.write('<p>" & CurrDir & "');" & return & "    document.write('<ul class=\"Links\">');" & return
	end if
	set TempText to TempText & "    document.write('<li>" & item a of FinalList & "</li>');" & return
	
end repeat
set TempText to TempText & "    document.write('</ul>');" & return
set TempText to TempText & "    document.close();"

set theFileContents to theFileContents & return & return & "function writelinks() {" & return
set theFileContents to theFileContents & TempText
set theFileContents to (theFileContents & "    document.write('<p>" & (((count of FinalList) / 2) as integer) as string) & " links <a href=\"http://homepage.mac.com/C1905266514/E209013910/index.html\">last updated</a> on " & (TheDate as string) & "</p>');" & return
set theFileContents to theFileContents & "}" & return


--Write the javascript file

--set theTargetFile to ((((path to current user folder) as string) & "Library:Application Support:iBlog:Javascript:safaribookmarks.js"))
set theTargetFile to (((path to desktop) as string) & "safaribookmarks.js") as file specification
try
	open for access theTargetFile with write permission
	set eof of theTargetFile to 0
	write (theFileContents) to theTargetFile starting at eof
	close access theTargetFile
	
on error
	try
		close access theTargetFile
	end try
end try

on searchReplace(theText, SearchString, ReplaceString)
	set OldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to SearchString
	set newText to text items of theText
	set AppleScript's text item delimiters to ReplaceString
	set newText to newText as text
	set AppleScript's text item delimiters to OldDelims
	return newText
end searchReplace