MACSCRPT Archives

August 2011

MACSCRPT@LISTSERV.DARTMOUTH.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Jon Pugh <[log in to unmask]>
Reply To:
Macintosh Scripting Systems <[log in to unmask]>
Date:
Fri, 26 Aug 2011 20:54:41 -0700
Content-Type:
text/plain
Parts/Attachments:
text/plain (63 lines)
On Aug 26, 2011, at 3:42 PM, Bob Rutledge wrote:
> I am looking at the SmartString script from Jon Pugh's site.  What is the usage 
> of this script?  Is it a set of string script routines you can cut and paste into 
> scripts as needed for working with strings?  

To quote from the comment block at the top of the SmartString script, I recommend using "load script" with some support code:

	-- The best way to use this script is to load it from disk when you compile 
	-- but check for updates when you run, like so:
	
	-- the alias follows the file if it's moved or renamed
	property ssFile : alias "YourDisk:Folder1:Folder2:SmartString"
	-- the modification date is burned in at compile time
	property ssDate : modification date of (info for ssFile)
	-- the script object is loaded at compile time too
	property ss : load script ssFile
	-- at run time check the mod date
	set modDate to modification date of (info for ssFile)
	-- if newer then load a new copy
	if modDate > ssDate then
		set SmartString to load script ssFile
		-- remember the date for the newly loaded file
		set ssDate to modDate
		--display dialog "I'm loaded"
	end if
	tell ss's SmartString
		setString("Your text here")
		-- do stuff
	end tell

Personally, I use it from Script Debugger, with the Library feature I keep a copy in ~/Library/Application Support/Script Debugger/Libraries and it is instantly available in any script by clicking the library toolbar button and choosing SmartString from the popup menu.

At that point, I can just "tell SmartString" and do anything I want with it.  For example, I needed to do some simple html character replacement the other day:

tell SmartString
	setString(urlString)
	replaceBetween("%22", "%22", textToInsert)
	replaceStrings(quote, "@@")
	replaceStrings("“", "@@")
	replaceStrings("”", "@@")
	replaceStrings("@@", "\\\"")
	replaceStrings("‘", "'")
	replaceStrings("’", "'")
	replaceStrings(space, "%20")
	replaceStrings("'", "%27")
	set urlString to getString()
end tell

Another time I needed to grab some info from a web page's source so I removed everything except what was in the <div> block I wanted:

tell SmartString
	setString(t)
	keepAfter("<div class=\"visualspoiler\">" & return & "    " & return)
	keepBefore("</div>")
	set t to getString()
end tell

If you use the recommended code from SmartString itself, you simply use the slightly indirect "tell ss's SmartString" or "tell SmartString of ss" if you are allergic to apostrophes.

What you want to accomplish with SmartString is up to you, of course, but there's a lot of useful routines there.  I've written them and used them, so they work pretty well and they make it real easy to write working scripts.

Jon

ATOM RSS1 RSS2