Some basic AppleScript advice.

Hello!

I dont really know where to go for some help, but here looks as good a place as any. I’m writing an Automator action and I’ve decided I need to use some AppleScript to filter out some text of a string. My text looks like:

What I want is to create an iCal event using the Date and Time in there, offsetting by -1 hour 30 mins if its BST, or 30 mins if its GMT. Sorry if this is a lot to ask, but I got no idea where to start. All I’ve done in the past is hacked around other peoples Applescripts to change it to how I want.

I guess I need an if statement, so I tried using the following in the AppleScript Action:

But it says it expects end where else is. I’m out of my depth here!

Heres my Automator workflow so far - http://www.squarefrog.co.uk/ebayical.zip. If you test it you’ll need to load a random ebay item (preferably from ebay.co.uk as thats what I intend to use) to get the string.

Thanks :smiley:

Hi squarefrog,

The problem with your script is that with an if/else block you need to put the actions on a new line.

On playing around I found a fairly easy way to extract the date from the string. This will only work if the text string is always in this format.

on run {input, parameters}
	set AppleScript's text item delimiters to {"-"}
	set tempStr to (words -7 thru -1 of input) as text
	set AppleScript's text item delimiters to {""}
	set theDate to date tempStr
	if word -1 of input is "BST" then
		set theDate to theDate - 90 * minutes
	else
		set theDate to theDate - 30 * minutes
	end if
end run

Best wishes

John M

My word that was quick!!! Thanks for that, I shall look into iCal scripting now (but problaly end up posting back here when I ultimately fail!)

I think I need to get a good book on how to interpret Application Dictionaries.

Ok I’ve already hit a brick wall… Using the script you provided i get the following error

Not sure exactly what this means…

ok, not a problem.

Notice the curly brackets {}. This means it’s a list (which only has one item). You’ll need to get the first item out of this list before doing the stuff I gave you before. So let’s change what I gave you to:


on run {input, parameters}
	set myItem to item 1 of input
	set AppleScript's text item delimiters to {"-"}
	set tempStr to (words -7 thru -1 of myItem) as text
	set AppleScript's text item delimiters to {""}
	set theDate to date tempStr
	if word -1 of myItem is "BST" then
		set theDate to theDate - 90 * minutes
	else
		set theDate to theDate - 30 * minutes
	end if
end run

Best

John M

Thanks again for the speedy reply. And thanks for telling me how you fixed the problem… its the only way to learn!

OK… so I’ve copy and pasted my way to success, using both John M’s code and the code from the ebaytoical script. Basically the reason I’m doing this is to give me something to use until the ebaytoical script author fixes his script!

Now all I need is a way to set the variable articleName to the title of the ebay auction. Is there a way to extract the title of a page easily? I need a way of extracting the text string between "eBay.co.uk: " and “(item” in the title. I guess this can be done using:


	set AppleScript's text item delimiters to {"-"}
  
                       (  then some code in here to copy whats left?   )

	set AppleScript's text item delimiters to {""}

Hi squarefrog,

I’m glad to see you’re almost there. If you don’t mind I have added a solution below that just uses Applescript.

I got all the info needed (apart from the URL) from the ‘title’ tag of the page. I did most of the work with TIDs.

TIDs (applescript’s text item delimiters) are what Applescript sticks between characters when you do something like:

set myText to "hel" & "lo"

With the TIDs set to normal {“”} (that is nothing) this would produce “hello”, but if the TIDs were set to {“man po”} the result would be “helman polo”. The TIDs are a list (the curly brackets) of one item (for historical reasons).

It also works the other way. If you get the text items of a string when you have set the TIDs to a substring you will get a list of two or more text strings. ie:

--Look at results of:
set stringA to "Something to demonstrate."
set AppleScript's text item delimiters to {" to "}
set theArray to text items of stringA
set AppleScript's text item delimiters to {""}
return theArray
--> {"Something", "demonstrate."}

It’s important to reset the TIDs when you have finished. Otherwise they will stay at the unusual setting and will come back to bite you later. (I hope some of this makes sense.)

So. This is what I did that seems to do what you want. I reset the 90/30 mins in advance stuff to 60/0mins depending on BST or not - as the event alarm is set 30mins in advance of the event time anyway.

-- Run when the Safari auction window is open and at the front
tell application "Safari"
	-- Get the url
	set theURL to URL of document 1
	-- Get file source
	set mySource to source of document 1
end tell

-- Extract contents of the <title> tag from the source
set AppleScript's text item delimiters to {"<title>"}
set myString to text item 2 of mySource
set AppleScript's text item delimiters to {"</title>"}
set myTitle to text item 1 of myString
-- Get the Description
set AppleScript's text item delimiters to {" ("}
set MySummary to text item 1 of myTitle
-- Get the end time string
set AppleScript's text item delimiters to {"end time"}
set MyTimeString to text item 2 of myTitle
set AppleScript's text item delimiters to {""}

-- Remive eBay,co.uk from string
set articleName to (characters 13 thru -1 of MySummary) as text
-- Make the time string into a date variable
set theDate to date MyTimeString
if word -1 of MyTimeString is "BST" then set theDate to theDate - 60 * minutes

-- Put it all into iCal
tell application "iCal"
	set eBayCal to (the first calendar whose title is "eBay")	
	set theEvent to make new event at end of events of eBayCal with properties {start date:theDate, end date:theDate, summary:articleName, url:theURL}
	tell theEvent to make new sound alarm at end of sound alarms with properties {trigger interval:-30}
end tell

I hope you don’t mind.

Ask if there is anything in this that doesn’t make sense.

John M

Of course I dont mind. I only started it in Automator because it was the only thing I know. Thanks for the awesome information on TIDs, I was a bit reluctant to post my problem here at first - I thought I’d either get no answers, or flamed for being a new applescripter. Instead I’ve got some great advice and a solution to my problem… what more could I ask for? :smiley:

How did you start learning AppleScript? Or what’s a good resource for learning? I thought about a good book or something. But I suppose once I know the basic arguments and how to read the dictionaries I wont need it any more. I’ve managed to teach myself PHP and Flash Actionscript, so AppleScript shouldnt really be too hard. The way I taught myself those languages was to read some basic information to work out the fundamentals, then downloading otherpeoples code and reading them/deconstructing them. The problem with doing this in AppleScript is that not many people comment their code!

Anyway, thanks again for your help I greatly appreciate it.

Hi,

I picked things up as I went along (this is the slow way).

There are a lot of resources on the internet for learning Applescript, this site not being the least. Try http://macscripter.net/links/#7 and http://macscripter.net/faq/

Buying a good book might be an idea. See http://macscripter.net/books/

If you want more advice on where to get started, I’d start a new thread in this forum.

Best wishes

John M

Thanks for that John, some good links there.

So here is the final script, saved as an application for when I find something I like on eBay.co.uk. I added some dialogs to alert me if the ebay page isnt selected, and to tell me when the scripts finished running.

-- Run when the Safari auction window is open and at the front
tell application "Safari"
	-- Get the url
	set theURL to URL of document 1
	-- Get file source
	set mySource to source of document 1
end tell
-- Display an error if the url does not contain "ebay"
if (not (theURL contains "ebay")) then
	display dialog "This does not look like an ebay page" with icon stop buttons {"Abort"}
	return
end if
-- Extract contents of the <title> tag from the source
set AppleScript's text item delimiters to {"<title>"}
set myString to text item 2 of mySource
set AppleScript's text item delimiters to {"</title>"}
set myTitle to text item 1 of myString
-- Get the Description
set AppleScript's text item delimiters to {" ("}
set MySummary to text item 1 of myTitle
-- Get the end time string
set AppleScript's text item delimiters to {"end time"}
set MyTimeString to text item 2 of myTitle
set AppleScript's text item delimiters to {""}

-- Remive eBay,co.uk from string
set articleName to (characters 13 thru -1 of MySummary) as text
-- Make the time string into a date variable
set theDate to date MyTimeString
-- if word -1 of MyTimeString is "BST" then set theDate to theDate - 60 * minutes  <<< not needed if your macs timezone is BST! D'OH!

-- Put it all into iCal
tell application "iCal"
	set eBayCal to (the first calendar whose title is "eBay")
	set theEvent to make new event at end of events of eBayCal with properties {start date:theDate, end date:theDate, summary:articleName, url:theURL}
	tell theEvent to make new sound alarm at end of sound alarms with properties {trigger interval:-30}
end tell
-- Show that the script has run successfully
display dialog "Event created successfully!" buttons {"OK"} default button "OK"