Problem when reading from textedit (New User)

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:

  1. How can I eliminate those weird setting in my textedit documents?
  2. Is there a faster way than keystrokes to get the URLs into the new tabs?
  3. Do any of you have ideas to enable the storing of multiple tab documents but retaining the user-friendliness?
  4. What general ways are there to improve the script’s elegance and/or performance?
  5. 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!

Hi,

first of all: Welcome to MacScripter :slight_smile:

Doesn’t the Safari menu item History > Reopen All Windows of Last Session the same as your script(s)?

PS:

the problem in your script is, that you save the text as RTF, which is plain text with format tokens.
It is much easier to save the URLs as plain text, then TextEdit is not needed at all.
Making Safari frontmost is also not necessary.

The following scripts do the same and are language independent.

Save script


-- This script saves the Tabs of your frontmost Safari window.
set tabdoc to ((path to documents folder as text) & "Tabs:Tabs1.txt")

tell application "Safari" to set turl to URL of every tab of window 1
try
	set ff to open for access file tabdoc with write permission
	write turl to ff as list
	close access ff
on error
	try
		close access file tabdoc
	end try
end try

Restore script


--This is the restoration script. 
set tabdoc to ((path to documents folder as text) & "Tabs:Tabs1.txt")
set theURLs to (read file tabdoc as list)
tell application "Safari"
	make new document
	set URL of document 1 to item 1 of theURLs
	repeat with i from 2 to (count theURLs)
		tell window 1 to make new tab with properties {URL:item i of theURLs}
	end repeat
end tell

First reply: It does a similar thing, and I also use it, but sometimes I just want to declutter my Safari windows and not quit the application. The script could be used, for example, to save a window in which I researched for a school project when I don’t need it for some days. In this case, the information about this window would be long lost.

Second reply: Thank you! :slight_smile: It seems so much easier than what I wrote and is much faster too. I’m not familiar with properties yet and also didn’t think about just making a new tab. Also, I missed that you could read files as list. Now I can put these scripts to full use.

Are txt documents the only ones that can be opened without also opening the corresponding applications?

Actually yes, AppleScript can open all files which contain plain text (including plist, xml, rtf, html).
The faceless application Image Events can open image files, and System Events can open QuickTime and Audio files to retrieve metadata information, but can’t play them