Hello,
I’m new to Applescript and thus can’t help with most problems here, so I’ll just come here for help/advice until I’m better. I wrote a basic tab saving/restoring script which can be activated via Quicksilver. It stores the URLs of the tabs in the currently active window in a textedit document, which is read by a second script and then loaded in Safari. But I had a problem when reading the document back to me; instead of only getting the text I want, there is a whole lot of text with font settings attached at the beginning. I got around it by putting a “<” before the URLs, a character which is forbidden in URLs. If you try to use the script, remember to change the user name according to yours, set the menu items to your language and specify a folder to save the document into (I used a “tabs” folder in the documents folder).
So my questions are:
- How can I eliminate those weird setting in my textedit documents?
- Is there a faster way than keystrokes to get the URLs into the new tabs?
- Do any of you have ideas to enable the storing of multiple tab documents but retaining the user-friendliness?
- What general ways are there to improve the script’s elegance and/or performance?
- How do you use the “get” command?
Several of these questions (especially the last one) might seem a little dumb for you, but I’m just starting writing scripts, so I really don’t know much. If you have any code suggestions, please explain them to me.
This is the code I wrote so far:
First, the tab saving script
-- This script saves the Tabs of your frontmost Safari window.
tell application "Finder"
set pro to first application process whose frontmost is true
set visible of pro to false
--hides the applescript program to show Safari
end tell
if frontmost of application "Safari" is true then
-- This is a very long conditional; it makes sure that the script won't do anything (and thus not produce errors) when you use it while Safari is not your active program. If you use Quicksilver (like me), you can specify that only Safari is able to use it when you press the according key combination
tell application "Safari"
set win to first window of application "Safari"
set turl to URL of every tab of win
--gets all tabs of your current window
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to ", "
set turl to "<" & turl as text
set AppleScript's text item delimiters to oldDelimiters
-- I put in the < because of an odd behaviour of Textedit, which, when read, puts loads of font, font size etc. preferences before the actual content. I read somewhere on the internet that the < character is not allowed in URLs, so it's a handy way to separate what you want and what can be forgotten.
end tell
if appIsRunning("TextEdit") then
-- This is for quitting TextEdit when I wasn't using it before activating the script. The handler can be found at the bottom of the document.
set status to "on"
else
set status to "off"
end if
tell application "TextEdit"
activate
if status is equal to "off" then
set visible of every window to false
--hide the empty window that appears when starting TextEdit, so it won't clutter my screen for too long
end if
set tabdoc to make new document
set visible of window 1 to false
--I also want to hide the new document
set paragraph 1 of tabdoc to turl
--write turl to tabdoc
close tabdoc saving yes saving in file "Macintosh HD:Users:(your user name):Documents:Tabs:Tabs1.rtf"
if status is equal to "off" then
quit
end if
end tell
end if
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
-- the handler for checking whether TextEdit is on. I got it from the first comment at http://codesnippets.joyent.com/posts/show/1124
Now, the tab restoring code
--This is the restoration script. You can change the path to your file according to your needs. I haven't yet tried to make more than one file, and I can't really think of a way to keep it both user-friendly and expand the functions.
set theTextFile to ("Macintosh HD:Users:(your user name):documents:tabs:tabs1.rtf")
try
set theFileRef to (open for access file theTextFile)
end try
try
set theText to item -1 of (read theFileRef for (get eof theFileRef) using delimiter {"<", "}"})
--what you get is a total mess; the urls are preceded by preferences for font, size, colour etc. - thus, it needs to be cleaned up
--second to last item because the } in the end has to be deleted
end try
close access theFileRef
--close file
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to ", "
set theText to every text item of theText
set AppleScript's text item delimiters to oldDelimiters
--the standard procedure for parsing a long string.
set ctext to the count of theText
--for the repeat loop
tell application "Safari"
activate
-- create all the tabs that are needed
tell application "System Events"
tell process "Safari"
-- I don't really understand the difference between a process and an application; this part of code is a blend of my own writing and stuff off of http://www.devdaily.com/blog/post/mac-os-x/applescript-open-multiple-urls-in-safari-tabs
click menu item "Neues Fenster" of menu "Ablage" of menu bar 1
-- 1.) the menu bar is not language-insensitive. Goddammit! You may have to change the item and menu names.
end tell
keystroke (item 1 of theText)
keystroke return
--enter url, press enter
repeat with i from 2 to (ctext)
-- for each additional url, a tab is created
tell process "Safari"
click menu item "Neuer Tab" of menu "Ablage" of menu bar 1
-- see 1.)
end tell
keystroke (item i of theText)
keystroke return
--same as above
end repeat
end tell
end tell
--return theText
-- i used this line for checking how far the compiler gets before an error occurs
If this is the wrong forum or the code tags are too long, please tell me and I’ll try to do something about it. Thank you in advance for replies of any kind!