Loading an Scriptfile and passing a variable to it - possible or not?

Hi Folks,

may this is possible or not - I am not sure wheter my syntax is correct - or maybe it is
not even with the correct syntax possible…

this is the file I am trying to load:


on myHandler()
set theFile to alias ((path to desktop as Unicode text) & "adressbook.txt")
set del_list to paragraphs of (read theFile)
repeat with i from 1 to count del_list
   if item i of del_list is delete_adressbook then
       set del_list to delete_list_item(del_list, i)
       exit repeat
   end if
end repeat
set {TID, text item delimiters} to {text item delimiters, return}
set del_list to del_list as Unicode text
set text item delimiters to TID
display dialog del_list
end MyHandler()

on delete_list_item(theList, theItem)
   if theItem is 1 then
       set q to rest of theList as text
   else if theItem is (count theList) then
       set q to items 1 thru -2 of theList as text
   else
       set q to (items 1 thru (theItem - 1) of theList) & (items (theItem + 1) thru -1 of theList) as text
   end if
end delete_list_item

Here is where the Variable has been built:


set delete_adressbook to delete_nachname & ";" & delete_vorname & ";" & delete_telefon & ";"

Here is where I load the script File:


set script_path to ((path to desktop as Unicode text) & "adressbook_delete.scpt")

set del_list to load script file script_path

tell del_list to myHandler()
display dialog del_list


When I run the code, than (when linking to the script file) than it comes up with an message: delete_adressbook not defined

I have put the delete_adressbook into a property - is it possible to pass a variable to an external script file, or shall I put the variable
into a text file, and read that string into my external script file?

Thanks for your help,

Stefan

Hi Stefan,

you can define a property within the external script

property delete_adressbook : ""

on myHandler()
set theFile to alias ((path to desktop as Unicode text) & "adressbook.txt")
.

and then to set the property and call the handler

set script_path to ((path to desktop as Unicode text) & "adressbook_delete.scpt")
set del_list to load script file script_path
tell del_list
	set its delete_adressbook to delete_nachname & ";" & delete_vorname & ";" & delete_telefon & ";"
	set del_result to myHandler()
end tell
display dialog del_result

Hi Stefan,

I have told you dozen times - but I have to tell you again…

You are amazing…

The Code that far:
Here is the xCode file…


	if name of theObject is "bt_delete_adressbook" then
		
		set delete_nachname to contents of text field "nachname" of drawer "Inbox" of window "main"
		set delete_vorname to contents of text field "vorname" of drawer "Inbox" of window "main"
		set delete_telefon to contents of text field "telefon" of drawer "Inbox" of window "main"
		
		set delete_adressbook to delete_nachname & ";" & delete_vorname & ";" & delete_telefon & ";"
		
		set script_path to ((path to desktop as Unicode text) & "adressbook_delete.scpt")
		set del_list to load script file script_path
		tell del_list
			set its delete_adressbook to delete_nachname & ";" & delete_vorname & ";" & delete_telefon & ";"
			set del_result to myHandler()
		end tell
		display dialog del_result
		
		set write_adressbook to del_list
		-- hier schreiben wir ins file - alles ausser dem gelöschten namen
		
		set fileToCheck_adressbook to ((path to desktop as Unicode text) & "adressbook.txt")
		
		set open_adressbook to open for access fileToCheck_adressbook with write permission
		write (del_result as string) to open_adressbook starting at 0
		close access open_adressbook
		-- hier wird wieder gelesen
		
		delay 1
		--hier lesen wir wieder aus dem file: 
		
		set theFile to alias ((path to desktop as Unicode text) & "adressbook.txt")
		set num_list to paragraphs of (read theFile)
		set myList_ab to {}
		set {tid, text item delimiters} to {text item delimiters, ";"}
		repeat with num in num_list
			set end of myList_ab to text items 1 thru 3 of num
		end repeat
		set text item delimiters to tid
		set content of table view "adressbook" of scroll view "adressbook" of drawer "Inbox" of window "main" to myList_ab
		
		
	end if

Here is the AS Script

property delete_adressbook : ""
on myHandler()
	set theFile to alias ((path to desktop as Unicode text) & "adressbook.txt")
	set del_list to paragraphs of (read theFile)
	repeat with i from 1 to count del_list
		if item i of del_list is delete_adressbook then
			set del_list to delete_list_item(del_list, i)
			exit repeat
		end if
	end repeat
	set {TID, text item delimiters} to {text item delimiters, return}
	set del_list to del_list as Unicode text
	set text item delimiters to TID
	display dialog del_list
end myHandler

on delete_list_item(theList, theItem)
	if theItem is 1 then
		set q to rest of theList
	else if theItem is (count theList) then
		set q to items 1 thru -2 of theList
	else
		set q to (items 1 thru (theItem - 1) of theList) & (items (theItem + 1) thru -1 of theList)
	end if
end delete_list_item

Thanks a lot for your help!

Stefan

Some suggestions for a little improvement

if name of theObject is "bt_delete_adressbook" then
	tell drawer "Inbox" of window "main"
		set delete_entry to contents of text field "nachname" & ";" & contents of text field "vorname" & ";" & contents of text field "telefon" & ";"
	end tell
	
	set script_path to ((path to desktop as Unicode text) & "adressbook_delete.scpt")
	set del_list to load script file script_path
	tell del_list
		set its delete_adressbook to delete_entry
		set del_result to myHandler()
	end tell
	display dialog del_result
	
	-- hier schreiben wir ins file - alles ausser dem gelöschten namen
	set fileToCheck_adressbook to ((path to desktop as Unicode text) & "adressbook.txt")
	
	set open_adressbook to open for access fileToCheck_adressbook with write permission
	write (del_result as string) to open_adressbook starting at 0
	close access open_adressbook
	set num_list to paragraphs of del_result -- no need to read again, the data are already in variable del_result
	set myList_ab to {}
	set {tid, text item delimiters} to {text item delimiters, ";"}
	repeat with num in num_list
		set end of myList_ab to text items 1 thru 3 of num
	end repeat
	set text item delimiters to tid
	set content of table view "adressbook" of scroll view "adressbook" of drawer "Inbox" of window "main" to myList_ab
end if

For the second script I strongly recommend the binary search version :wink:

Hi Stefan,

I will do as you recommended - but when I try to save the file, I get an NSInternalScriptError (8) error - but the syntax and the file should be ok…

Best regards and thanks a lot!

Stefan

Hi Stefan,

do you mean this way?


property delete_adressbook : ""
on myHandler()
	
	set theFile to alias ((path to desktop as Unicode text) & "adressbook.txt")
	set del_list to paragraphs of (read theFile)
	set foundIndex to BinarySearch(del_list, delete_adressbook)
	if foundIndex is not false then set del_list to delete_list_item(del_list, foundIndex)
	
	set {TID, text item delimiters} to {text item delimiters, return}
	set del_list to del_list as Unicode text
	set text item delimiters to TID
	display dialog del_list
end myHandler
on delete_list_item(theList, theItem)
	if theItem is 1 then
		return rest of theList
	else if theItem is (count theList) then
		return items 1 thru -2 of theList
	else
		return (items 1 thru (theItem - 1) of theList) & (items (theItem + 1) thru -1 of theList)
	end if
end delete_list_item

on BinarySearch(theList, theValue)
	local low, mid, high
	script o
		property l : theList
	end script
	
	set low to 1
	set high to (count theList)
	repeat while (low ≤ high)
		set mid to (low + high) div 2
		if ({theValue} is in items low thru mid of o's l) then
			set high to mid - 1
		else
			set low to mid + 1
		end if
	end repeat
	
	if (item low of o's l is theValue) then return low
	return false
end BinarySearch

Best Regards,

Stefan

Hi Stefan,

I have found the problem - but I am not quite sure how to solve it:

The Variable “del_result” will be shown from the loaded script file, but not passed
to the XCode File…



--this is the XCode File

 set script_path to ((path to desktop as Unicode text) & "adressbook_delete.scpt")
       set del_list to load script file script_path
       tell del_list
           set its delete_adressbook to delete_nachname & ";" & delete_vorname & ";" & delete_telefon & ";"
           set del_result to myHandler()
       end tell
       display dialog del_result --this one is empty!


and here the adressbook_delete.scpt


property delete_adressbook : ""
on myHandler()
   
   set theFile to alias ((path to desktop as Unicode text) & "adressbook.txt")
   set del_list to paragraphs of (read theFile)
   set foundIndex to BinarySearch(del_list, delete_adressbook)
   if foundIndex is not false then set del_list to delete_list_item(del_list, foundIndex)
   
   set {TID, text item delimiters} to {text item delimiters, return}
   set del_list to del_list as Unicode text
   set text item delimiters to TID
   display dialog del_list
end myHandler
on delete_list_item(theList, theItem)
   if theItem is 1 then
       return rest of theList
   else if theItem is (count theList) then
       return items 1 thru -2 of theList
   else
       return (items 1 thru (theItem - 1) of theList) & (items (theItem + 1) thru -1 of theList)
   end if
end delete_list_item

on BinarySearch(theList, theValue)
   local low, mid, high
   script o
       property l : theList
   end script
   
   set low to 1
   set high to (count theList)
   repeat while (low ≤ high)
       set mid to (low + high) div 2
       if ({theValue} is in items low thru mid of o's l) then
           set high to mid - 1
       else
           set low to mid + 1
       end if
   end repeat
   
   if (item low of o's l is theValue) then return low
   return false
end BinarySearch

Thanks for your help!

STefan

just add

return del_list

at the end of the myHandler() handler


.
set del_list to del_list as Unicode text
set text item delimiters to TID
display dialog del_list
return del_list
end myHandler
.