Called script

I have a script that I call from another script. If I don’t call it it runs great but if called I get the following error – Can’t get item 1 of {}.

Being a beginner I am sure I am doing something stupid but I can figure out why it will run if not called and fail if called??
The only thing I do is comment out the on run and end run statement of the called program and uncomment out the set myCitType lines.

thanks for help
Roger


on run {myCitType}
– for testing only
–set myCitType to “BK”

global myCitCount
set myCitCount to ""
global myCitRecords
set myCitRecords to {}
global myCitation
set myCitation to ""


-- Format Citation for outpput
GetLoadCitRecords(myCitType)

-- format citation
set myRepeat to 1

repeat
	-- get record to format
	set myRec to item myRepeat of myCitRecords -- <<<<<-- this is where the error occurs 
	
	-- do function getFormat
	GetFormat(myRec)
	
	-- increment myRepeat to get next record
	set myRepeat to myRepeat + 1
	
	-- check if Done button pushed
	set myDone to item 8 of myRec
	if myDone = "Done" then exit repeat
	
end repeat

--send finished formated citation to clipboard
set the clipboard to myCitation

end run

–***************************************************************************************
– Sub Load Citation Records
–***************************************************************************************

on GetLoadCitRecords(myCitType)

-- define fields
set myCitRecords to {}
set myCitCount to ""

-- Set values for each record
if myCitType = "BK" then
	set myRec001 to {"(BK) ", "001", "Page Number ", "Page Number:", ", ", "attribute", "N", "Next"}
	set end of myCitRecords to myRec001
	set myCitCount to myCitCount + 1
	set myRec001 to {"(BK) ", "002", "Person of Interest ", "Person of Interest:", ", ", "attribute", "Y", "Next"}
	set end of myCitRecords to myRec001
	set myCitCount to myCitCount + 1
	set myRec001 to {"(BK) ", "003", "(recorded ", "Date:", ")", "Date", "Y", "Done"}
	set end of myCitRecords to myRec001
	set myCitCount to myCitCount + 1
end if

return

end GetLoadCitRecords

–***************************************************************************************
– Format Record based on passed variables function
–***************************************************************************************

on GetFormat(myRec)

-- set fields
set myCit to ""

-- set display of mandatory field
if item 7 of myRec = "Y" then
	set myMandatory to "**"
else
	set myMandatory to ""
end if

-- set up for date defalut
if item 6 of myRec = "Date" then
	--Format current date
	set myYear to get the (year of (current date)) as string
	set myMonth to get the (month of (current date)) as string
	set myDay to get the (day of (current date)) as string
	set myCit to myDay & " " & myMonth & " " & myYear
end if

-- repeat if option field is "Y" and field is blank
repeat
	
	set tempVar to display dialog item 1 of myRec & myMandatory & "Enter " & item 4 of myRec default answer myCit buttons {"Restart", "Cancel", item 8 of myRec} default button item 8 of myRec
	
	set myCit to text returned of result
	
	if myCit ≠ "" then
		set myCitation to myCitation & item 3 of myRec & myCit & item 5 of myRec
	end if
	
	-- check to see if option field is not "N" or if field has data
	if item 7 of myRec = "N" then exit repeat
	
	-- exit repeat if 
	if myCit ≠ "" then exit repeat
	
end repeat

end GetFormat

This is the calling program


– Set myType to citation type
set myType to “BK”

– Run Citation Formater Script
run script file “Macintosh HD 2:RGS:RGSFormater.scpt” with parameters {myType}

– say finished
say “Finished”

Hi Roger.

There are a couple of things going on here.

Globals have to be declared at the very top of the script, not in the ‘run’ handler, for them to be usable everywhere in the script. Alternatively, you can declare them in just the handlers which need to share them ” eg. ‘myCitRecords’ in both the ‘run’ handler and in ‘GetLoadCitRecords’. But it’s best to avoid using globals where possible. ‘GetLoadCitRecords’ could return the value of its ‘myCitRecords’ and the one in the ‘run’ handler could be set to the result.

You explicitly initialise your repeat variable ‘myRepeat’ to 1 and then explicitly increment it in a bare ‘repeat’ loop. The error was being thrown because the ‘run’ handler’s ‘myCitRecords’ was still set to {} (owing to the ‘global’ issue) and didn’t have an item 1. If you were to set ‘myRepeat’ to increment automatically .

repeat with myRepeat from 1 to (count myCitRecords)

. the loop wouldn’t be executed at all if ‘myCityRecords’ was empty, although there might be another error later on, of course.

I would have rewritten the script to demonstrate how to do without globals, but I don’t have time to make sure I don’t muck it up for you! The immediate cure for your problem is to put the global declarations at the top of the script, but the values must still be set within the handlers.

Hello.

That is not exactly how it works on my machine, but you may have meant something else, for all I know.

It doesn’t work, if the variable isn’t declared global from the handlers, from which it is accessed.


property tlvl : me
property myOtherScript : load script (alias ((path to temporary items from user domain as text) & "otherscript.scpt"))
script myscript
    on run
        global myvar
        
        set myvar to 5
        tell tlvl's myOtherScript to run
        log myvar
    end run
end script

tell myscript to run

This is the other script:

on run
    global myvar
    log myvar
    set myvar to 6
end run

This is the log:

--started (*5*) (*6*) -- stopped

That’s exactly how it works here. Changing the top of the called script to the following cures the problem immediately, no further edits required:

global myCitCount, myCitRecords, myCitation

on run {myCitType}
	-- for testing only
	--set myCitType to "BK"
	
	--global myCitCount
	set myCitCount to ""
	--global myCitRecords
	set myCitRecords to {}
	--global myCitation
	set myCitation to ""

. which sounds pretty much like what I wrote:

Agreed :slight_smile: I missed the “alternatively part”. :lol:

And sorry for the trouble.

Thanks for the reply Nige

Your advice is greatly appreciated. As a newbee it is easy to become discouraged. I have done some simple scripts but this is the first one on any significance that I have tried. When I posted the question I had been working for hours trying to figure it out. With your suggestion I now feel that I am not so dumb after all. I do not necessasarly want someone to rewrite my code but to do just as you did and make suggestions.

Putting the global at the top before the on run made the script work as I had wanted. I am going to go back and reread the language guide about handlers as I did not think of the to run as a handler.

I am also going to try and figure out how to get rid of the globals as you suggested.

Again
Thanks