Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > AppleScript - Keeping a variable

AppleScript - Keeping a variable
Thread Tools
Addicted to MacNN
Join Date: Sep 2000
Status: Offline
Reply With Quote
Oct 28, 2001, 04:25 PM
 
I have a script that goes something like this (though this is an example just to illustrate my question, and isn't really doing anything)

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
on run {}
do_function1()
do_function2()
end run

on do_function1()
set x to <font color = blue>1</font>
end do_function1

on do_function2()
set final_result to x + <font color = blue>1</font>
display dialog final_result
end do_function2
</font>[/code]

Now if you actually ran that script you would notice that x is undefined in do_function2(). Does anyone know how too keep the variable x so that it is available in any sub process?
I always use protection when fscking my Mac... Do you?
     
Grizzled Veteran
Join Date: Oct 1999
Location: Minneapolis
Status: Offline
Reply With Quote
Oct 29, 2001, 03:32 AM
 
Yes, define is as a property in the beginning of the script:

property x : ""

Moving to devolpers fora
     
Mac Enthusiast
Join Date: Jan 2001
Location: Most probably sitting down, London, European Union
Status: Offline
Reply With Quote
Oct 29, 2001, 06:04 AM
 
Originally posted by macvillage.net:
<STRONG>I have a script that goes something like this (though this is an example just to illustrate my question, and isn't really doing anything)

&lt;BLOCKQUOTE&gt;&lt;font size="1"face="Geneva, Verdana, Arial"&gt;code:&lt;/font&gt;&lt;HR&gt;&lt;pre&gt;& amp;lt;font size=1 face=courier&gt;
on run {}
do_function1()
do_function2()
end run

on do_function1()
set x to &lt;font color = blue&gt;1&lt;/font&gt;
end do_function1

on do_function2()
set final_result to x + &lt;font color = blue&gt;1&lt;/font&gt;
display dialog final_result
end do_function2
&lt;/font&gt;&lt;/pre&gt;&lt;HR&gt;&lt;/BLOCKQUOTE&gt;
</STRONG>

What Oscar wrote is correct, if "x" doesn't change due to input If it does you need to do it another way, let's assume x gets passed back to the main script from do_function1 and then on to do_function2:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
on run {}
set x to do_function1()
do_function2(x)
end run

on do_function1()
set x to <font color = blue>1</font>
return x
end do_function1

on do_function2()
set final_result to x + <font color = blue>1</font>
display dialog final_result
end do_function2</font>[/code]

There are other variants on this, hope this makes sense.


-- Clive

[ 10-29-2001: Message edited by: Clive ]
     
Addicted to MacNN
Join Date: Sep 2000
Status: Offline
Reply With Quote
Oct 29, 2001, 06:14 PM
 
Great!


Now how would I perform the following:

I have a file similar to an XML file (though not quite) with a few lines:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
&lt;Data1&gt;Something&lt;/Data1&gt;
&lt;Data2&gt;Something Else&lt;/Data2&gt;
&lt;Data3&gt;More Junk&lt;/Data3&gt;
</font>[/code]

I can use something like:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
open for access file myFile
set datacontents to (read file myFile to (get eof file myFile))
close access file myFile
</font>[/code]

to read the file, but then comes the question of processing.

I need to:

set data1 to (whatever is between &lt;data1&gt; and &lt;/data1&gt;
set data2 to (whatever is between &lt;data2&gt; and &lt;/data2&gt;
set data3 to (whatever is between &lt;data3&gt; and &lt;/data3&gt;

Any clues to this?
I always use protection when fscking my Mac... Do you?
     
Mac Enthusiast
Join Date: Jan 2001
Location: Most probably sitting down, London, European Union
Status: Offline
Reply With Quote
Oct 29, 2001, 08:13 PM
 
You can do it with raw AppleScript, but I don't think you really want to. Try this http://www.osaxen.com/?id=xml_tools


-- Clive
     
Addicted to MacNN
Join Date: Sep 2000
Status: Offline
Reply With Quote
Oct 29, 2001, 08:16 PM
 
What would be the raw way? I would rather not use more scripting additions than necessary.
I always use protection when fscking my Mac... Do you?
     
Mac Enthusiast
Join Date: Jan 2001
Location: Most probably sitting down, London, European Union
Status: Offline
Reply With Quote
Oct 29, 2001, 08:40 PM
 
Originally posted by macvillage.net:
<STRONG>What would be the raw way? I would rather not use more scripting additions than necessary.</STRONG>
No, you want to use the OSAX!

Do you know about text item delimiters? If not, you don't really want to get into them and trying to pass stuff around subroutines. Really hard to debug when they go wrong.


-- Clive
     
Addicted to MacNN
Join Date: Sep 2000
Status: Offline
Reply With Quote
Oct 29, 2001, 08:46 PM
 
I was thinking of basing it on something like this:
http://www.tidbits.com/resources/481/UASYahoo.html
I always use protection when fscking my Mac... Do you?
     
Mac Enthusiast
Join Date: Jan 2001
Location: Most probably sitting down, London, European Union
Status: Offline
Reply With Quote
Oct 29, 2001, 08:53 PM
 
Originally posted by macvillage.net:
<STRONG>I was thinking of basing it on something like this:
http://www.tidbits.com/resources/481/UASYahoo.html</STRONG>
Well it basically tells you how to do it there, but I think it's more than a little messy. Really, if you haven't done that much AppleScript, especially text processing, I'd go with the OSAX. Actually I've done *loads* of this type of thing for search and replace routines... and I'd get the OSAX.

It says freeware, so why not?
     
Addicted to MacNN
Join Date: Sep 2000
Status: Offline
Reply With Quote
Oct 29, 2001, 08:55 PM
 
Originally posted by Clive:
<STRONG>

Well it basically tells you how to do it there, but I think it's more than a little messy. Really, if you haven't done that much AppleScript, especially text processing, I'd go with the OSAX. Actually I've done *loads* of this type of thing for search and replace routines... and I'd get the OSAX.

It says freeware, so why not?</STRONG>
Becuase then deployment is more work.
I always use protection when fscking my Mac... Do you?
     
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Oct 30, 2001, 01:54 AM
 
Here is a very basic example of what you can do.. all it does is get rid of the &lt;data&gt; stuff and then seperates by returns. If the number of data is always different then you'll need to set up a messy repeat loop.. anyways...

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>open for access file myFile
set datacontents to (read file myFile to (get eof file myFile))
close access file myFile

set searchString to {<font color = red>"&lt;Data1&gt;"</font>, <font color = red>"&lt;/Data1&gt;"</font>, <font color = red>"&lt;Data2&gt;"</font>, <font color = red>"&lt;/Data2&gt;"</font>, <font color = red>"&lt;Data3&gt;"</font>, <font color = red>"&lt;/Data3&gt;"</font>}
set replaceString to {<font color = red>""</font>, <font color = red>""</font>, <font color = red>""</font>, <font color = red>""</font>, <font color = red>""</font>, <font color = red>""</font>}

repeat with i from <font color = blue>1</font> to length of searchString
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to (item i of searchString)
set tempList to text items of datacontents
set AppleScript's text item delimiters to (item i of replaceString)
set datacontents to tempList as string
set AppleScript's text item delimiters to oldDelim
end repeat

set oldelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set temp to (every text item of datacontents)
set AppleScript's text item delimiters to oldelim

set data1 to item <font color = blue>1</font> of temp
set data2 to item <font color = blue>2</font> of temp
set data3 to item <font color = blue>3</font> of temp</font>[/code]
     
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Oct 30, 2001, 02:08 AM
 
Okay here you go, this should work if there is more than one pieces of data in the text. It is really sloppy and could probably be better. It will screw up if the data itself has <font face = "courier">&lt;</font> or a <font face = "courier">&gt;</font> in it. If this is really a problem then it can be fixed by doing it differently.


<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>open for access file myFile
set datacontents to (read file myFile to (get eof file myFile))
close access file myFile

set tempData to {}

set thedata to my stringToList(datacontents, return)

repeat with i from <font color = blue>1</font> to length of thedata
set end of tempData to item <font color = blue>1</font> of (my stringToList(item <font color = blue>2</font> of (my stringToList(item i of datacontents, <font color = red>"&gt;"</font>)), <font color = red>"&lt;"</font>))
end repeat

tempData

on stringToList(theString, delim)
tell AppleScript
set oldelim to text item delimiters
set text item delimiters to delim
set temp to (every text item of theString)
set text item delimiters to oldelim
end tell
return temp
end stringToList</font>[/code]
     
Mac Enthusiast
Join Date: Jan 2001
Location: Most probably sitting down, London, European Union
Status: Offline
Reply With Quote
Oct 30, 2001, 11:51 AM
 
I'd do something like the first way Synotic demo'd. Though it relies on your data tags being unique.
     
Senior User
Join Date: Oct 2000
Location: oakland, ca usa
Status: Offline
Reply With Quote
Oct 30, 2001, 01:32 PM
 
I have a script that goes something like this (though this is an example just to illustrate my question, and isn't really doing
anything)

code:


on run {}
do_function1()
do_function2()
end run
on do_function1()
set x to 1
end do_function1
on do_function2()
set final_result to x + 1
display dialog final_result
end do_function2

Now if you actually ran that script you would notice that x is undefined in do_function2(). Does anyone know how too keep the
variable x so that it is available in any sub process?
if i read this right, you don't need persistant data (saved to file or property), but merely a 'global x' line at the toip of the script.

global x

on run {}
do_function1()
do_function2()
end run
on do_function1()
set x to 1
end do_function1
on do_function2()
set final_result to x + 1
display dialog final_result
end do_function2

that should be all you need for this example.
     
Addicted to MacNN
Join Date: Sep 2000
Status: Offline
Reply With Quote
Oct 30, 2001, 03:58 PM
 
Originally posted by Synotic:
<STRONG>Okay here you go, this should work if there is more than one pieces of data in the text. It is really sloppy and could probably be better. It will screw up if the data itself has &lt;font face = "courier"&gt;&lt;&lt;/font&gt; or a &lt;font face = "courier"&gt;&gt;&lt;/font&gt; in it. If this is really a problem then it can be fixed by doing it differently.


&lt;BLOCKQUOTE&gt;&lt;font size="1"face="Geneva, Verdana, Arial"&gt;code:&lt;/font&gt;&lt;HR&gt;&lt;pre&gt;& amp;lt;font size=1 face=courier&gt;open for access file myFile
set datacontents to (read file myFile to (get eof file myFile))
close access file myFile

set tempData to {}

set thedata to my stringToList(datacontents, return)

repeat with i from &lt;font color = blue&gt;1&lt;/font&gt; to length of thedata
set end of tempData to item &lt;font color = blue&gt;1&lt;/font&gt; of (my stringToList(item &lt;font color = blue&gt;2&lt;/font&gt; of (my stringToList(item i of datacontents, &lt;font color = red&gt;"&gt;"&lt;/font&gt; )), &lt;font color = red&gt;"&lt;"&lt;/font&gt; ))
end repeat

tempData

on stringToList(theString, delim)
tell AppleScript
set oldelim to text item delimiters
set text item delimiters to delim
set temp to (every text item of theString)
set text item delimiters to oldelim
end tell
return temp
end stringToList&lt;/font&gt;&lt;/pre&gt;&lt;HR&gt;&lt;/BLOCKQUOTE&gt;</STRONG>
Ok, I like this one, and it seems that it could work, but my script is currently worked so that this whole thing is between:

on auto_update()

end auto_update

so when it gets to: on stringToList(theString, delim) that is when the trouble begins.

The file it needs to process is at: http://software.accettura.com/pub/au...laryngitis.xml

It is a butchered up XML file.

It needs to turn:
&lt;currentversion&gt;1.0&lt;/currentversion&gt;

into:
set currentversion to "1.0"
I always use protection when fscking my Mac... Do you?
     
Admin Emeritus
Join Date: Nov 2000
Location: New Yawk
Status: Offline
Reply With Quote
Oct 30, 2001, 04:47 PM
 
Moving this thread to OS X Developer forum because Developer forum has been axed. All future developer questions of any kind should be submitted to OS X Developer or Web Developer.

thanks
"Do not be too positive about things. You may be in error." (C. F. Lawlor, The Mixicologist)
     
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Oct 30, 2001, 07:49 PM
 
Originally posted by macvillage.net:
<STRONG>

Ok, I like this one, and it seems that it could work, but my script is currently worked so that this whole thing is between:

on auto_update()

end auto_update

so when it gets to: on stringToList(theString, delim) that is when the trouble begins.

The file it needs to process is at: http://software.accettura.com/pub/au...laryngitis.xml

It is a butchered up XML file.

It needs to turn:
&lt;currentversion&gt;1.0&lt;/currentversion&gt;

into:
set currentversion to "1.0"</STRONG>
You cannot have an "on handler" inside another "on handler".. put the stringToList routine on the bottom of the script.

It is currently impossible to generate variables based on strings... however even if you could it would most likely complicate things. Just set up a repeat loop and refer to them by index. Can you show us what you are going to do with the data?
     
Addicted to MacNN
Join Date: Sep 2000
Status: Offline
Reply With Quote
Oct 30, 2001, 08:42 PM
 
Originally posted by Synotic:
<STRONG>You cannot have an "on handler" inside another "on handler".. put the stringToList routine on the bottom of the script.
</STRONG>
Already did that, and for now it works good.


Originally posted by Synotic:
<STRONG>It is currently impossible to generate variables based on strings... however even if you could it would most likely complicate things. Just set up a repeat loop and refer to them by index. Can you show us what you are going to do with the data?</STRONG>
Well here is what I got:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
open for access file myFile
set datacontents to (read file myFile to (get eof file myFile))
close access file myFile
set searchString to {<font color = red>"&lt;currentversion&gt;"</font>, <font color = red>"&lt;/currentversion&gt;"</font>, <font color = red>"&lt;releasedate&gt;"</font>, <font color = red>"&lt;/releasedate&gt;"</font>, <font color = red>"&lt;downloadurl&gt;"</font>, <font color = red>"&lt;/downloadurl&gt;"</font>}
--set replaceString to {<font color = red>"\"</font>", <font color = red>"\"</font>, <font color = red>", "</font>\<font color = red>""</font>, <font color = red>"\"</font>, <font color = red>", "</font>\<font color = red>""</font>, <font color = red>"\"</font> <font color = red>", "</font>\<font color = red>""</font>, <font color = red>"\"</font>"}
set replaceString to {<font color = red>""</font>, <font color = red>""</font>, <font color = red>""</font>, <font color = red>""</font>, <font color = red>""</font>, <font color = red>""</font>}
repeat with i from <font color = blue>1</font> to length of searchString
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to (item i of searchString)
set tempList to text items of datacontents
set AppleScript's text item delimiters to (item i of replaceString)
set datacontents to tempList as string
set AppleScript's text item delimiters to oldDelim
end repeat
set oldelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set pretemp to (every text item of datacontents)
set temp to pretemp
set AppleScript's text item delimiters to oldelim
set au_current_version to item <font color = blue>1</font> of temp
set au_release_date to item <font color = blue>2</font> of temp
set au_download_url to item <font color = blue>3</font> of temp
</font>[/code]

my problem right now is turning temp into a string.
I always use protection when fscking my Mac... Do you?
     
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Oct 31, 2001, 12:35 AM
 
To turn anything into a string just do... get temp as string
     
Addicted to MacNN
Join Date: Sep 2000
Status: Offline
Reply With Quote
Oct 31, 2001, 04:13 PM
 
Ok, I made a few changes to non apple script things, and that freed up a lot of options.

I can now set that online file to anything I want. It doesn't have to be XML like, but can be setup anyway needed.

I was thinking maybe just delimiting it something like

value1,value2,value3

would that maybe make it easier? Could I then just:

set tempdata to (datacontents as string)


Obviously I don't want to actually write:
set x to "value1"
set y to "value2"
set z to "value3"

that would create huge security hole. Anything put in that file would execute on all computers with the script.
I always use protection when fscking my Mac... Do you?
     
Mac Elite
Join Date: Oct 2000
Status: Offline
Reply With Quote
Oct 31, 2001, 05:10 PM
 
Originally posted by macvillage.net:
<STRONG>Ok, I made a few changes to non apple script things, and that freed up a lot of options.

I can now set that online file to anything I want. It doesn't have to be XML like, but can be setup anyway needed.

I was thinking maybe just delimiting it something like

value1,value2,value3

would that maybe make it easier? Could I then just:

set tempdata to (datacontents as string)


Obviously I don't want to actually write:
set x to "value1"
set y to "value2"
set z to "value3"

that would create huge security hole. Anything put in that file would execute on all computers with the script.</STRONG>
I am kind of confused.. when you read from the file you will get "value1,value2,value3" ? When you read from a file it is already a string unless you use the "using delimiter" property.

Could you restate your goal again?..
     
Addicted to MacNN
Join Date: Sep 2000
Status: Offline
Reply With Quote
Oct 31, 2001, 05:21 PM
 
I have a file on a remote server:

must state:

variable1 is "X"
variable2 is "Y"
variable3 is "Z"

It can be formatted anyway needed, XML, "|" delimited, it doesn't matter.

The only thing I DON'T want to do is:

set variable1 to "X"
set variable2 to "Y"
set variable3 to "Z"

and just read and execute

The reason being is I don't want the security hole. If someone hacks the fileserver and modifies that file, they can execute code on many other computers, so if it is something that variables are extracted from, then nobody can run any code.

So it needs to proces something containing the necessary data.
I always use protection when fscking my Mac... Do you?
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:54 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2