Try to run on your mac?

Hi,

I just made a script and somehow it only works after each time I edit it.

I find that this is very particular and i think it’s related to my mac.
therefore i ask you to open this in your standard applescript editor and run it.
The script does not change anything on your computer or send any information. As you can see.

The script is supposed to tell if a url begins with ftp://.
so takes characters one thru six and puts them in a variable, sometime’s as (ftp://), but other times (and i don’t know why) it puts them in the variable as

(*f/t/p/:////*)

.

The rest of the script extracts the ip address from the URL. but I’m still working on that and you don’t really need to look at it. The only reason i put that up is to because it MIGHT strangely affect the weird output
Please reply me your findings
Thanks in advance!

global location
global ftperror
global notftp
set location to "ftp://127.0.0.1/path/to/url/"
set notftp to "The URL needs to be FTP for this version of vlow"
testftp_(location)
testIP_()
on testftp_(a)
	set a to a as text
	set checkFTP to characters 1 thru 6 of a as text
	log checkFTP
	if "ftp://" is in checkFTP then
		set ftperror to "no"
	else
		display dialog notftp
		
	end if
end testftp_

on testIP_()
	if ftperror = "no" then
		stripIP_()
	end if
end testIP_
on stripIP_()
	delimiters_("/")
	set IpAddress to text item 3 of location
	log IpAddress
end stripIP_
on delimiters_(a)
	set a to a as text
	if a = missing value then
		return AppleScript's text item delimiter
	else
		set AppleScript's text item delimiters to a
	end if
end delimiters_

on checkDNS(a)
	set badAns to a & " is not a valid DNS entry. Please try again."
	set ASTD to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "."
	set howMany to count (text items of a)
	if howMany < 4 then
		display dialog "Whoops"
		return
	end if
	set TI to text items of a
	set AppleScript's text item delimiters to ASTD
	set x to 1
	repeat while x ≤ howMany
		--display dialog text item x of a
		tell (text item x of TI) as number
			if it ≥ 0 and it < 256 then
				set x to (x + 1)
				set msg to "Yay"
			else
				set msg to badAns
				exit repeat
				--getDns()
			end if
		end tell
	end repeat
	display dialog msg
end checkDNS


I got “The URL needs to be FTP for this version of vlow”. OS X 10.5.8, IntelMac.

Before asking people to run a script, you should explain what it’s supposed to do and where it’s going wrong. Otherwise your post sounds just like a dodgy e-mail.

That’s what i keep getting if i don’t edit anything, srsly anything at all in the script (even adding a comment helps) but when i run it twice it doesn’t work… weird

mmm. you are right, ill edit right away

Hello, try creating a run handler.

Declare your global variables as local within the run handler, and pass the ftperror to testIP_() just to remove any problems with properties being saved to disk between runs.

Edit And it doesn’t hurt to set Applescript’s text item delimiters to “” at the start of your script either. :wink:

I think that you some how doesn’t reset Applescript’s text item delimiters when your script terminates.

The script doesn’t restore AppleScript’s text item delimiters when it’s finished with them. Their value is “/” when it finishes and the next time you run it. Since you use ‘characters 1 thru 6 of a as text’ in the first handler, the “/” is interpolated between every character of checkFTP and it doesn’t match the protocol with which you’re comparing it.

You should:

  1. Reset the delimiters when you’ve finished with them.
  2. Use ‘text 1 thru 6 of a’ instead of ‘characters 1 thru 6 of a as text’.

There’s a typo in the delimiters_() handler where you’ve missed off the ‘s’ in the first instance of ‘text item delimiters’.

  • Nigel - I Did what you said and it worked. Normally i would have reset the delimiter but when i don’t use some parts of applescript i just forget do to that. I remember a topic in MacScripter / unScripted about this that i read quite some time ago.

  • McUsr - As i’m self-learning ASOC with macscripter and other sources i haven’t come to the run handler yet. After i finish this script i will be looking into it!

Thank you both for your help, it worked

For the record, this is the working script:

-- Begin of Script --
global location
global ftperror
global notftp
set location to "ftp://127.0.0.1/path/to/url/"
set notftp to "The URL needs to be FTP for this version of vlow"
testftp_(location)
testIP_()
on testftp_(a)
	set a to a as text
	set checkFTP to text 1 thru 6 of a as text
	log checkFTP
	if "ftp://" is in checkFTP then
		set ftperror to "no"
	else
		display dialog notftp
		
	end if
end testftp_

on testIP_()
	if ftperror = "no" then
		stripIP_()
	end if
end testIP_
on stripIP_()
	delimiters_("/")
	set IpAddress to text item 3 of location
	log IpAddress
	delimiters_("")
end stripIP_
on delimiters_(a)
	set a to a as text
	if a = missing value then
		return AppleScript's text item delimiters
	else
		set AppleScript's text item delimiters to a
	end if
end delimiters_

Btw typo fixed

Hello!

You are in fact using a run handler in your script right now. I didn’t understand your problem at first. Beware that globals that are declared at the top level of your script in your explicit or implicit run handler are properties that will be stored on disk if your applescript runner supports storing. If you then copy a global, you may perform a recursive copy, adding more and more context to your globals for each time your script is run and in the end it may fail to run due to an internal table overflow. :slight_smile:

The second problems with globals is that they may lock your script with certain values ” closures, when used in context with script objects in your scripts.

And that is how i remember that im not that good at programming :0.

to prevent the internal table overflow i guess i have to reset all value’s to missing value

You really shouldn’t have to worry about it, just use local variables in your run handler, and pass arguments to your other handlers.

It is totally ok, to store static, “constant like” values as properties.

The thing to avoid, is to use the copy command together with global values and properties.

Live by those rules, and it will be a happy life! :smiley: