Problem with set command in Applescript (plus file path question)

I have a script that is supposed to gather some values from different fields on a web page and add them in a CSV file. But something is halting it when preparing to append it to the file.

The script in all its glory is below and it is supposed to work with all ongoing tennis matches here: http://www.betfair.com/exchange/tennis Set the tennis match as the frontmost window in safari

global kampkob1, kampkob2, kampsalg1, kampsalg1, matchet, spiller1, spiller2, theString, filePath
set filePath to ("Macintosh HD:Brugere:anders:Skrivebord:" as text) & "kamp.csv"
tell application "System Events"
	tell process "Safari"
		try
			set kampkob1 to name of button of UI element 2 of row 3 of table 1 of group 7 of group 1 of group 3 of UI element 1 of scroll area 1 of group 1 of group 1 of group 2 of window 1
			set kampkob2 to name of button of UI element 2 of row 4 of table 1 of group 7 of group 1 of group 3 of UI element 1 of scroll area 1 of group 1 of group 1 of group 2 of window 1
			set kampsalg1 to name of button of UI element 3 of row 3 of table 1 of group 7 of group 1 of group 3 of UI element 1 of scroll area 1 of group 1 of group 1 of group 2 of window 1
			set kampsalg2 to name of button of UI element 3 of row 4 of table 1 of group 7 of group 1 of group 3 of UI element 1 of scroll area 1 of group 1 of group 1 of group 2 of window 1
			set matchet to value of static text of group 1 of UI element 1 of row 2 of table 1 of group 7 of group 1 of group 3 of UI element 1 of scroll area 1 of group 1 of group 1 of group 2 of window 1
			set spiller1 to value of static text of group 9 of group 3 of group 2 of group 1 of group 3 of UI element 1 of scroll area 1 of group 1 of group 1 of group 2 of window 1
			set spiller2 to value of static text of group 9 of group 4 of group 2 of group 1 of group 3 of UI element 1 of scroll area 1 of group 1 of group 1 of group 2 of window 1
			set theString to kampkob1 & kampkob2 & kampsalg1 & kampsalg2 & matchet & spiller1 & spiller2
		end try
	end tell
end tell
set theResult to writeTo(filePath, theString, text, true)
if not theResult then display dialog "There was an error writing the data!"

on writeTo(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as text
		set openFile to open for access file targetFile with write permission
		if apendData is false then set eof of openFile to 0
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
		try
			close access file targetFile
		end try
		return false
	end try
end writeTo

The first half until the “end tells” my own while the rest is more or less a direct copy from a script by regulus 6633 in the thread here http://macscripter.net/viewtopic.php?id=36861. “My” part delivers a string like

{“1.07 1612”, “13.5 1241”, “1.08 15518”, “15.5 111”, "Matchet: ", “DKK 390.035”, “15”, “15”}

to the rest of the script but somehow somethings goes wrong already in the first line after “end tells”. Any suggestion on what I do wrong. Plus is my file path in the correct format? I have tried using “Users” and “Desktop” instead of “Brugere” and “Skrivebord” but it seems like it makes no difference.

Thank you.

Browser: Safari 600.1.25
Operating System: Mac OS X (10.8)

Hi,

while developing scripts it’s strongly recommended to comment out all try blocks to get error messages.
I guess that the index numbers in all UI scripting lines are missing e.g. (name of button 1 of UI element . )

Btw: tell blocks make your script a bit more readable


.
		-- try
		tell group 1 of group 3 of UI element 1 of scroll area 1 of group 1 of group 1 of group 2 of window 1
			tell table 1 of group 7
				set kampkob1 to name of button of UI element 2 of row 3
				set kampkob2 to name of button of UI element 2 of row 4
				set kampsalg1 to name of button of UI element 3 of row 3
				set kampsalg2 to name of button of UI element 3 of row 4
				set matchet to value of static text of group 1 of UI element 1 of row 2
			end tell
			tell group 2
				set spiller1 to value of static text of group 9 of group 3
				set spiller2 to value of static text of group 9 of group 4
			end tell
		end tell
		set theString to kampkob1 & kampkob2 & kampsalg1 & kampsalg2 & matchet & spiller1 & spiller2
		-- end try
.

Thanks StefanK.

I don´t think it has anything to do with “my” half part of the code. It delivers a string every time, for instance right now:

{“1.43 849”, “3.25 3916”, “1.44 8839”, “3.35 755”, "Matchet: ", “DKK 1.247.597”, “0”, “0”} for

http://www.betfair.com/exchange/tennis/event?id=27307357&exp=e

and that is exactly what I need

Thats not to say it doesn´t somehow create havoc in the rest of the script

I did do some “manual debugging” before posting and forgot include the result, sorry. If I cut anything below

set theResult to writeTo(filePath, theString, text, true)

I get “«script» forstÃ¥r ikke “writeTo”-beskeden.” which should be something like “«script» deosn´t understand the “writeTo”-message”

Thats why I have strong feeling the problem lies around that line.

That’s not a string – that’s a list of strings.

Thanks and sorry. Another thing I didn´t mention: I tried :

set theString to kampkob1 & "," & kampkob2 & "," & kampsalg1 & "," & kampsalg2 & "," & matchet & "," & spiller1 & "," & spiller2 as string

Result: “1.76 280,2.1 808,1.9 893,2.3 214,Matchet: DKK 3.009.212,30,0”

But it doesn´t change the error for

set theResult to writeTo(filePath, theString, text, true)

this coerces only the last element to string not the whole expression.
Add parentheses

set theString to (kampkob1 & kampkob2 & kampsalg1 & kampsalg2 & matchet & spiller1 & spiller2) as string

The output reveals eight different strings but only 7 requests. Probably there’s something wrong with the first request.
Using the & character If the second item can not be coerced to the class of the first item, the expression returns a list

I really doubt that the lack of parenthesis makes a difference. The output and error message s the same with or without.

The reason the output at first look doesn´t match the number of requests is because the four first requests returns a string that includes a space

1.42 1667
3.15 237
1.46 169
3.4 696

That combined without seperation becomes

1.42
16673.15
2371.46
1693.4
696

I have now included , to separate each request in the list. It still doesn´t change the error. Could it be the spaces? And if so how do I change them?

set the theString to replace_chars(theString, " ", ",")

doesn´t seem to work.

As now, included house keeping and suggestion the script is currently (with the same error message):

global kampkob1, kampkob2, kampsalg1, kampsalg1, matchet, spiller1, spiller2, theString, writeTo
set filePath to ("Macintosh HD:Brugere:anders:Skrivebord:" as text) & "kamp.csv"
tell application "System Events"
	tell process "Safari"
		try
			tell group 1 of group 3 of UI element 1 of scroll area 1 of group 1 of group 1 of group 2 of window 1
				tell table 1 of group 7
					set kampkob1 to name of button of UI element 2 of row 3
					set kampkob2 to name of button of UI element 2 of row 4
					set kampsalg1 to name of button of UI element 3 of row 3
					set kampsalg2 to name of button of UI element 3 of row 4
					set matchet to value of static text of group 1 of UI element 1 of row 2
				end tell
				tell group 2
					set spiller1 to value of static text of group 9 of group 3
					set spiller2 to value of static text of group 9 of group 4
				end tell
			end tell
			set the theString to (kampkob1 & "," & kampkob2 & "," & kampsalg1 & "," & kampsalg2 & "," & matchet & "," & spiller1 & "," & spiller2) as string
			
		end try
	end tell
end tell

I have now tried to cut out the first five requests from the string,

set theString to (spiller1 & "," & spiller2) as string

alternatively

set theString to (spiller1,spiller2) as string

leaving only the point scores with a comma between them. theString will now always be in the form of “0,15”, “30,40” etc and never contain spaces.

Still the same error message with both solutions.

with your two game cut insert this line before the set theString line to get the classes of the results


display dialog "spiller1 : " & class of spiller1 & return & "spiller2 : " & class of spiller2

They come out as lists

I am grateful for the help I am getting and I admit I have really limited knowledge of applescript. But please explain to me what you are trying to show me here.

If I ask for the class of theString (the variable that carries the requests into the rest of the script) it is text and should´t that be what matters?

And if I add “as strings” to the spiller1 and spiller2 requests they also come out as text and I still get the same error message.

Put this on hold. It looks like the file path is the problem…

the & character is used to concatenate strings but also to build lists and records.

If the leftmost item is a list all other items are added to the list regardless of their class
If the leftmost item is a string all other items are concatenated unless one of the items is no string or cannot be coerced to string, then the result is also a list

In post #3 ” which was edited a few seconds after post #4 was added ” the error message is described as:

This suggests that the immediate problem is something happening to prevent the script from recognising the handler. I can’t see what this could be from the posted code. Common causes are:

  1. Treating the handler label as a variable elsewhere in the script and setting it to another value.
  2. Misspelling the label in the call to the handler.
  3. Not including the handler in the code being tested.
  4. The handler label being a keyword in an installed OSAX.

But none of these seem to apply here. :confused:

If you do get this sorted out, bear in mind that writing the text ‘as text’ to the file will write UTF-16 Unicode text, probably big-endian, without a BOM. This is fine as long as whatever reads the file can deal with the data. Otherwise you may want to consider writing ‘as string’ (which the File Read/Write commands interpret as meaning single-byte characters in the Mac Roman character set) or ‘as «class utf8»’ (which is of course interpreted as meaning UTF-8 Unicode).

Edit: Corrected misspelling of “misspelling”! :rolleyes:

Thank you all. Turned out the only problem was the file path :confused: I only changed the system language to danish a few month ago to be able to quickly help my parents with their mac over the phone. Apparently Applescript doesn´t see the danish values for the standard folders…

This is language, startup disk and user independent


set filePath to (path to desktop folder as text) & "kamp.csv"

Hello Stefan

Some times, the target volume is not the startup one, so the path must be hard coded.
This case, it’s really easy to treat :

never use the localized names of the folders, use the English ones.
In fact, the system work with these English ones, it uses the localized strings only when it display them in the GUI.

At this time, my boot volume is a SSD in a Firewire enclosure (my old fingers are unable to change the disk in the iMac).
When I ask AppleScript to return the path to a file stored on the internal mechanical HD, it gives something like :

Macintosh HD:Users::Desktop:test-versions.rtf

As you see, it doesn’t use “Utilisateurs” but “Users”
it doesn’t use “Bureau” but “Desktop”.

Yvan KOENIG (VALLAURIS, France) dimanche 16 novembre 2014 19:43:15

I know, but in this particular case it is :wink: