Tell me whats wrong.

I have a script that is put on a flash drive. You would open the app and it would calibrate variables to come up with paths to folders to install stuff in and on error keeps popping up. This app has to be as FLEXIBLE as it can be.

property flashState : false
set currentDisks to paragraphs of (do shell script "ls /Volumes")
repeat with i in currentDisks
	if i ends with "_INSTALL" then
		set flashName to i as string
	end if
end repeat
tell application "Finder"
	repeat with i in (paragraphs of (do shell script "ls /Volumes/" & flashName & "/"))
		if i ends with "_INSTALL" then
			set AppleScript's text item delimiters to "_INSTALL"
			tell me to set _ to count (text items of i)
			set install to text items 1 thru (_ - 1) of i
			set installfolloc to POSIX file "/Volumes/" & flashName & "/" & i
		end if
	end repeat
end tell
set myApp to my name

if (flashName is in currentDisks) then
	if (flashState is false) then
		set flashdrive to POSIX file "/Volumes/" & flashName
		display dialog "Your flash drive has been connected? Install \"" & install & "\" to your computer?"
		set flashState to true
	end if
else
	if flashState is true then
		
		set flashState to false
	end if
end if

The main problem is the “count” on line 12 where it says

tell me to set _ to count (text items of i)

HELP!

DUH! I had to add " as list" to that line!

Here is the final script:

property flashState : false
set currentDisks to paragraphs of (do shell script "ls /Volumes")
repeat with i in currentDisks
	if i ends with "_INSTALL" then
		set flashName to i as string
	end if
end repeat
set l to true
repeat with i in (paragraphs of (do shell script "ls /Volumes/" & flashName & "/"))
	if i ends with "_INSTALL" and l then
		set AppleScript's text item delimiters to "_INSTALL"
		tell me to set _ to count (text items of i as list)
		set install to text items 1 thru (_ - 1) of i
		set AppleScript's text item delimiters to missing value
		set installfolloc to ((POSIX file "/Volumes/" & flashName & ":" & i) as string)
		set l to false
	end if
end repeat
set myApp to my name

if (flashName is in currentDisks) then
	if (flashState is false) then
		set flashdrive to POSIX file "/Volumes/" & flashName
		display dialog "Your flash drive has been connected. Install \"" & install & "\" to your computer?"
		tell application "Finder" to if not (exists alias ":Applications:" & install) then duplicate installfolloc to alias ":Applications:"
		set_item_name(alias (":Applications:" & (install & "_INSTALL") as string), install)
		set flashState to true
	end if
else
	if flashState is true then
		
		set flashState to false
	end if
end if
on set_item_name(this_item, new_item_name)
	tell application "Finder"
		--activate
		set the parent_container_path to (the container of this_item) as text
		if not (exists item (the parent_container_path & new_item_name)) then
			try
				set the name of this_item to new_item_name
			on error the error_message number the error_number
				if the error_number is -59 then
					set the error_message to "This name contains improper characters, such as a colon (:)."
				else --the suggested name is too long
					set the error_message to error_message -- "The name is more than 31 characters long."
				end if
				--beep
				tell me to display dialog the error_message default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
				copy the result as list to {new_item_name, button_pressed}
				if the button_pressed is "Skip" then return 0
				my set_item_name(this_item, new_item_name)
			end try
		else --the name already exists
			--beep
			tell me to display dialog "This name is already taken, please rename." default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
			copy the result as list to {new_item_name, button_pressed}
			if the button_pressed is "Skip" then return 0
			my set_item_name(this_item, new_item_name)
		end if
	end tell
end set_item_name

Just to be clear, it’s not normally necessary to write text items of i as list, since text items are returned as a list anyway. But the reference being passed to count is too complex for it to resolve by itself: ie. every text item of item of every paragraph of . The as list simply resolves the reference into a list of text items which is then passed to count to count. It’s quite inefficient, because all the paragraphs of the shell script result are extracted every time round the repeat and the text items of the relevant one are physically manifested too. (Twice in consecutive lines!)

I’d suggest:

  1. Put get in front of paragraphs of at the top of the repeat so that they’re an actual list rather than just a reference and are only got once.

2(a). Use count text items of contents of i. contents of will resolve i to the approprate paragraph, count text items will count the text items without actually manifesting them as a list first.

2(b). Don’t count the text items at all, since the result’s only used to calculate the index of the penultimate one, which can be indexed as -2:
set install to text items 1 thu -2 of i.

It’s a bit worrying that the list install is then inserted into texts while the text item delimiters are missing value.

I did what you did, and it gave me the same problem I had before, it copied the “name_INSTALL” (of course “name” can be anything) appeared a few times… Wait.

Now it works. HUH? :o
It didn’t work one time, now it does. Now it works.
OK. Thanks for the tip.