fix a problem panther has caused

hi -

I use a script that saves and then opens your saved tabs for safari acquired it originaly from MacOSX Hints until i upgraded to panther. now it no longer works properly.

the problem is occurring when saving tabs for the 2nd time or more. the script is supposed to be clearing the contents of a text file where the url’s are saved… however in panther it is not… the script just keeps adding the urls to the end of it or not being overwritten… i have encountered both.

this is the section of code that seems to be the problem:

– erase current contents of file:
set eof of open_file to 0
write url_list to open_file starting at eof
close access open_file


this is the “save tabs” script:

– localizations
property windowMenu : “Window”
property nextTabMenuItem : “Select Next Tab”
– end localizations

property url_list : {}
property docCount : 0

tell application “Safari”
activate
set docCount to count of documents
end tell

tell application “System Events”
tell process “Safari”

set menuItemCount to count of menu items of ¬
menu windowMenu of menu bar 1
set menuItemOffset to menuItemCount - docCount

– count the windows
set winCount to count of windows

– loop through the windows
repeat with i from 1 to winCount

set winTitle to title of window i

if (count of characters of winTitle) > 20 then
set winTitle to text 1 thru 20 of winTitle
end if

repeat with j from menuItemOffset + 1 to menuItemCount
if ((title of menu item j of menu windowMenu of menu bar 1) ¬
starts with winTitle) then
set itemNum to j
exit repeat
end if
end repeat

– activate a window
click menu item itemNum of menu windowMenu of menu bar 1

– check for browser window
if (count of radio buttons of window i) > 0 then

– activate the first tab
click radio button 1 of window i

– url of first tab
set firstUrl to value of text field 1 of group 1 ¬
of splitter group 1 of window i

– save the url
set url_list to url_list & firstUrl

my nextTab()

– url of the next tab
set nextUrl to value of text field 1 of group 1 ¬
of splitter group 1 of window i

repeat until firstUrl is equal to nextUrl

– save the last url
set url_list to url_list & nextUrl

my nextTab()

– url of next tab
set nextUrl to value of text field 1 of group 1 ¬
of splitter group 1 of window i

end repeat

– empty line to seprate windows
set url_list to url_list & “”

end if

end repeat
end tell
end tell

– convert url list to text
set old_delim to AppleScript’s text item delimiters
set AppleScript’s text item delimiters to return
set url_list to url_list as text
set AppleScript’s text item delimiters to old_delim

– get path to prefs file where URLs will be stored
set prefs_folder to path to preferences folder as string
set prefs_file to prefs_folder & “Safari Saved URLs”

try
set open_file to ¬
open for access file prefs_file with write permission
– erase current contents of file:
set eof of open_file to 0
write url_list to open_file starting at eof
close access open_file
on error
try
close access file prefs_file
end try
end try

– let the user know we are done.
tell application “Safari”
activate
display dialog “All Done” buttons {“OK”} default button 1
end tell

on nextTab()
tell application “Safari” to activate
tell application “System Events”
tell process “Safari”
click menu item nextTabMenuItem of ¬
menu windowMenu of menu bar 1
end tell
end tell
end nextTab


this is the “open tabs” script:

tell application “Safari” to activate

– get path to prefs file where URLs are stored
set prefs_folder to path to preferences folder as string
set prefs_file to prefs_folder & “Safari Saved URLs”

try
– read the saved urls
set open_file to ¬
open for access file prefs_file without write permission
set url_list to read open_file using delimiter return
close access open_file

set tmpList to {}
set newUrl_list to {}

– make urls from file into a list of lists
– each window is a seperate list in the big list
repeat with aUrl in url_list
if aUrl & “x” is “x” then
set newUrl_list to newUrl_list & {tmpList}
set tmpList to {}
else
set tmpList to tmpList & aUrl
end if
end repeat

– don’t forget the last list, or maybe the only
set newUrl_list to newUrl_list & {tmpList}

tell application “Safari”

– loop through the list of windows
repeat with urls in newUrl_list

my new_window()
set emptyWindow to true

– loop through the list of tabs
repeat with aUrl in urls
if emptyWindow then
set emptyWindow to false
else
my new_tab()
end if

– open the url
set URL of document 1 to aUrl

end repeat

end repeat
end tell
on error
try
close access file prefs_file
end try
end try

– let the user know we are done.
tell application “Safari”
activate
display dialog “All Done” buttons {“OK”} default button 1
end tell

on new_tab()
tell application “Safari” to activate
tell application “System Events”
tell process “Safari”
click menu item “New Tab” of menu “File” of menu bar 1
end tell
end tell
end new_tab

on new_window()
tell application “Safari” to activate
tell application “System Events”
tell process “Safari”
click menu item “New Window” of menu “File” of menu bar 1
end tell
end tell
end new_window


can anyone help me straighten this out?

Thanks.

Um… I don’t think this is a Panther related error. Try this.

  1. Open all of the tabs you want.
  2. Run the SAVE TABS Script
  3. Close all Safari Related
  4. Run the OPEN TABS Script
  5. Close a few Safari Tabs
  6. Run the SAVE TABS Script
  7. Run the OPEN TABS Script

My guess is that you were leaving the original tabs open and the SAVE TABS script was cycling through EACH WINDOW and capturing all the associated tabs.

Now… After step 7, if you re-run the SAVE TABS and OPEN TABS again, you will see two sets of windows… but that’s because you had more than one Set open.

hi -

i have done as you suggested however this is not the problem. if you look at the “saved safari url’s” file in your home/library/preferences folder you will see that regardless of whether or not you change how safari’s tabs are represented when i resave in PANTHER it continues to add onto the end of the file and is not deleting it’s contents.

for example:
http://msn.espn.go.com/
http://sports.espn.go.com/ncb/index
http://sports.espn.go.com/nba/index
http://sports.espn.go.com/nba/index
http://www.ovolab.com/phlink/http://www.naratt.com/FolderIconX.htmlhttp://www.macosxhints.com/article.php?story=20030913153245341http://bbs.applescript.net/viewtopic.php?t=6006http://niknud.com/links.phphttp://www.babineau.ca/journal/index.phphttp://www.amazon.com/gp/registry/registry.html/102-1281878-4596107?id=1OJDLXJTAN0DNhttp://www.moveabletype.org/http://msn.espn.go.com/http://espn.go.com/boxing/news/2003/1213/1685711.htmlhttp://sports.espn.go.com/nfl/columns/story?columnist=pasquarelli_len&id=1685424http://espn.go.com/moresports/news/2003/1213/1685672.html

the first 4 urls are the 1st time i ran this script in panther. each mashed up url is a saved tab via the script since the first time… they are just getting slapped onto the end of the list.

i have tried deleting this file and it keeps returning… even after emptying the trash…

now i have gone a step further and changed the name of the file to be saved to “safari saved url’s2”

same problem exists. i saved 4 tabs… no problem. go to save the next group of tabs and they get mashed onto the end of the file:

http://bbs.applescript.net/viewforum.php?f=2
http://www.facespan.com/facespan/pagespeed/url//Title/
http://bbs.applescript.net/memberlist.php
http://niknud.com/index.phphttp://macsoftgames.com/products/halo/MacSoft-Halo.htmlhttp://www.executivesync.com/http://www.softobe.com/products/flsy/pp.html

any ideas? this never happened in panther nor did i ever need to close my safari session for this to work.

thanks!

At the top of your save tabs script, just after the properties have been defined, add this line to clear the URLs on each run of the script:

Jon


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

Try turning on the event log and then running the save script. Let us know what the tail part of the EVENT log reads (where it actually writes to the file)

Jon -

Thank you very much, that snippet of code solved the problem!
Have a happy holiday.

:smiley:

Hi,

I’m using the same scripts from macOSXhints and had the same problems. Jon’s tip with “set url_list to {}” solved a part of my problems: the file gets overwritten and new URLs are divided by returns.
But: Only the tabs in the frontmost window are written ti the file. If I have multiple windows open with multiple files, then the other windows are ignored. The next window gets touched by the save script but the URLs won’t get written to the file. The “Saved Safari URLs” file looks like this:

----BEGIN
http://daringfireball.net/2003/05/safaris_unscriptable_tabs
http://daringfireball.net/2003/01/scripting_safari_urls
http://daringfireball.net/2003/02/save_and_restore_safari_urls
http://www.my-two-cents.de/
http://bbs.applescript.net/viewtopic.php?t=6006

http://daringfireball.net/2003/05/safaris_unscriptable_tabs

----END

As you can see, the script writes a blank line as it should do and then the first URL (which was the first in the frontmost window) is getting repeatet.

Any tips?

Thanks,
Stefan