Scope Problem with script library (accessing TV app)

Hi i am currently trying to organize my Applescript into script libraries and am now stumbling across a -1708 scope error. What am i doing wrong?
Find the folllwowing sample programm (saved as app) with a local duplicate of the sampleLib handlers

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
use script "sampleLib"

--
-- main programm
--


try
	tell application "TV"
		set theTrackList to (get every track of library playlist 1)
		set thecurrTrack to (get first item of theTrackList)
		set thecurrTrackLocation to (location of thecurrTrack)
		set thecurrTrackLocationString to (thecurrTrackLocation as string)
	end tell
	
	log "main before start of calls"
	
	set theLibTrackLocationModifyResult to modifyAliasString_local(thecurrTrackLocationString)
	log "main after modifyAliasString_local call"
	
	set theLibTrackLocationResult to getTrackLocationString_local(thecurrTrack)
	log "main after getTrackLocationString_local call"
	
	set theLibTrackLocationModifyResult to modifyAliasString (thecurrTrackLocationString)
	log "main after modifyAliasString call to script library"
	
	set theLibTrackLocationResult to getTrackLocationString (thecurrTrack)
	log "main after getTrackLocationString call to script library"
	
on error errtext number errnum
	log ("Errnum: " & errnum & " -- " & errtext)
end try

--
-- end of main programm
--

on getTrackLocationString_local(theTrack)
	log "getTrackLocationString_local before tell"
	
	tell application "TV"
		log "getTrackLocationString_local after tell"
		set theResult to (location of theTrack) as string
		log "getTrackLocationString_local after accessing location"
	end tell
	
	log "getTrackLocationString_local after end tell"
	return theResult
end getTrackLocationString_local

on modifyAliasString_local(theAliasString)
	log "modifyAliasString_local before tell"
	
	set theResult to "Result.mp4"
	
	log "modifyAliasString_local after changing string"
	
	return theResult
end modifyAliasString_local

here is the “sampleLib” content

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions


on getTrackLocationString (theTrack)
	
	log "getTrackLocationString before tell"
	
	tell application "TV"
		log "getTrackLocationString after tell"
		set theResult to (location of theTrack) as string
		log "getTrackLocationString after accessing location"
	end tell
	
	log "getTrackLocationString after  end tell"
	
	return theResult
end getTrackLocationString

on modifyAliasString (theAliasString)
	
	log "modifyAliasString before tell"
	
	set theResult to "Result.mp4"
	
	log "modifyAliasString after changing string"
	
	return theResult
end modifyAliasString

the resulting logOutput is

(*main before start of calls*)
(*modifyAliasString_local before tell*)
(*modifyAliasString_local after changing string*)
(*main after modifyAliasString_local call*)
(*getTrackLocationString_local before tell*)
(*getTrackLocationString_local after tell*)
(*getTrackLocationString_local after accessing location*)
(*getTrackLocationString_local after end tell*)
(*main after getTrackLocationString_local call*)
(*modifyAliasString before tell*)
(*modifyAliasString after changing string*)
(*main after modifyAliasString call to script library*)
(*Errnum: -1708 -- „TV“ hat einen Fehler erhalten: file track id 20425 of library playlist id 11 of source id 10 versteht die Nachricht „getTrackLocationString“ nicht.*)

so thescript library is found and the modifyAliasString handler in the script library was called successful. How can I provide a handler working with TV Track in a script library?

Thanks for any hints
Austin

Hi Austin. Welcome to MacScripter!

The “app” script you’ve posted doesn’t contain any calls to a library. You may have more success like this:

use AppleScript version "2.4"
-- use framework "Foundation" -- Not needed in the posted script.
use scripting additions
use sampleLib : script "sampleLib" -- Load the script into a variable ('sampleLib' here).

--
-- main programm
--


try
	tell application "TV"
		set theTrackList to (get every track of library playlist 1)
		set thecurrTrack to (get first item of theTrackList)
		set thecurrTrackLocation to (location of thecurrTrack)
		set thecurrTrackLocationString to (thecurrTrackLocation as string)
	end tell
	
	log "main before start of calls"
	
	-- Refer to the handler as belonging to the library script.
	set theLibTrackLocationModifyResult to sampleLib's modifyAliasString(thecurrTrackLocationString)
	log "main after modifyAliasString call to script library"
	
	set theLibTrackLocationResult to sampleLib's getTrackLocationString(thecurrTrack)
	log "main after getTrackLocationString call to script library"
	
on error errtext number errnum
	log ("Errnum: " & errnum & " -- " & errtext)
end try

--
-- end of main programm
--

Or perhaps like this:

use AppleScript version "2.4"
-- use framework "Foundation" -- Not needed in the posted script.
use scripting additions
use sampleLib : script "sampleLib" -- Load the script into a variable ('sampleLib' here).

--
-- main programm
--


try
	tell application "TV"
		set theTrackList to (get every track of library playlist 1)
		set thecurrTrack to (get first item of theTrackList)
		set thecurrTrackLocation to (location of thecurrTrack)
		set thecurrTrackLocationString to (thecurrTrackLocation as string)
	end tell
	
	log "main before start of calls"
	
	tell sampleLib
		set theLibTrackLocationModifyResult to its modifyAliasString(thecurrTrackLocationString)
		log "main after modifyAliasString call to script library"
	
		set theLibTrackLocationResult to its getTrackLocationString(thecurrTrack)
		log "main after getTrackLocationString call to script library"
	end tell
	
on error errtext number errnum
	log ("Errnum: " & errnum & " -- " & errtext)
end try

--
-- end of main programm
--

Hi Nigel, thanks a lot for your suggestion, but both solution run into a compile error with something like “expression awaited but command found” and "command can not follow “its” ". to be more precise on the setup - “sampleLib” - the second code element i posted is a seperate file saved as .scptd compiled script bundle with an .sdef suite definition of the two handlers this work fine for all commands and handlers with are not using a TV track as parameter. I tried something in addition,

set theLibTrackLocationModifyResult to modifyAliasString (thecurrTrack)

also leads to an error

«event SAMPmtvA» file track id 20425 of library playlist id 11 of source id 10
(*Errnum: -1708 -- „TV“ hat einen Fehler erhalten: file track id 20425 of library playlist id 11 of source id    10 versteht die Nachricht „modifyAliasString“ nicht.*)

so probably this is more about a problem with passing the parameter by reference instead of by value. Is there any common pattern for that?

here is a screenshot of the dictionary content of sampleLib as additional explanation

Hmmm, you may be right.
Maybe AppleScript can’t pass non-native classes to a script library, even tho you immediately jump back into a proper tell block.
Try modifying your logs statements to log the class of the variables passed (as text).

I can’t test it myself right now. I only have my iPad with me

So i tried it the call

set theLibTrackLocationResult to getTrackLocationString_local(thecurrTrack)

leads to to log output

(*getTrackLocationString_local before tell*)
get class of file track id 20425 of library playlist id 11 of source id 10
(*getTrackLocationString_local class of param theTrack is: file track*)
(*getTrackLocationString_local after tell*)

the call

set theLibTrackLocationModifyResult to modifyAliasString (thecurrTrackLocationString)

leads to the log output

(*modifyAliasString before tell*)
(*modifyAliasString class of param theParam is: text*)
(*modifyAliasString after changing string*)

and the call

set theLibTrackLocationModifyResult to modifyAliasString (thecurrTrack)

leads to the log output

«event SAMPmtvA» file track id 20425 of library playlist id 11 of source id 10
(*Errnum: -1708 -- „TV“ hat einen Fehler erhalten: file track id 20425 of library playlist id 11 of source id 10 versteht die Nachricht „modifyAliasString“ nicht.*)

“SAMPmtvA” is the code of the handler “modifyAliasString” defined in the dictionary of the script bundle.
It seems that it fails with the call of the handler itself because no log statement is provided for the call.

if found a dirty workaround -
using a list of theTrack as parameter and accessing the first item in the list to resolve track info like this

set theLibTrackLocationResult to getTrackLocationString ({thecurrTrack})

but that is really dirty - is there a more smart solution?

Sorry, @Austin_Powers. I didn’t notice yesterday that the handlers in your first script had different names from those in the library script. I’ve now removed the calls to them from my suggestions above. However, I also didn’t appreciate what all the ‘log’ commands were showing or know that the library script provides a dictionary.

I’m afraid I can’t make out what the problem is at the moment. :frowning:

thank for getting back on that. looking forward for a clean solution now - a workaround is in place

I had to run the error thru a translator. The error is because you have a space between the subroutine/function name and the parameters.

i.e.

set theLibTrackLocationResult to getTrackLocationString (thecurrTrack). — bad

set theLibTrackLocationResult to getTrackLocationString(thecurrTrack). — good

(I think)

Robert, the space is added during compiling again. The script bundle is providing commands which are as i understand always have at least one direct parameter (or none) and all parameters are separated by space. But anyway - using the same syntax with text params or list params is working - i am not able to write a script calling the script bundle without the space.

That’s weird.
On my American English version of AppleScript, I have never been able to compile with the space.

Is it because your using a German language version?

I think it’s because of the setup of the command Suite using the script bundle - the functions are as i understand all recognized as scripting additions and therefore commands like “get every” which are also separeted with spaces from their params. But this can be a misinterpretation of me. This only occurs wenn you save a script bundle with and .sdef file which defines the handlers as commands as i understood

Ah, a clue

I wonder if it’s an mis-configured sdef?

I wouldn’t know because I’ve never made one before.

Try making a library without one and also not a script bundle but a regular .scpt

if i am just loading a script everything should be fine it’s quite similar to the local handlers then, but i m trying to build a set of libraries to encapsulate as much as possible all the functions .This also leads to a hierarchy in lib usage. And in addition the defining of Dictionaries is conveniant in terms of easy access in the handler syntax. As i said - i would like to stick to the concept and would rather like to understand what i am doing wrong so that the workaround

{theTrack}

as param is not necessary

I meant my comment as a diagnostic method to see if the sdef is the culpri.

yeah, thanks but if that would be the problem i would think that even the call with a text param should result in an error but if there is another problem with the .sdef file and i need to change something find the sdef file included below - i anyone knows - do i need to refer to another param type when using a TVtrack ad param (or itunes Track)?

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
<dictionary title="Dictionary">
    <suite name="SampleLib suite" code="SAMP" description="Contains sample handlers.">
        <class name="reference" code="obj " hidden="yes">
            <synonym name="specifier"/>
        </class>
        
        
        <command name="getTrackLocationString" code="SAMPgtvA" description="provides a string of the location attribute in a given TV track information according to the info level.">
            <direct-parameter description="the track">
                <type type="any">
                </type>
            </direct-parameter>
            

            
            
            
            <result description="location alias as string">
                <type type="string">
                </type>
            </result>
        </command>
        
        <command name="modifyAliasString" code="SAMPmtvA" description="takes any element as parameter and returns a sample string .">
            <direct-parameter description="the string">
                <type type="any">
                </type>
            </direct-parameter>
            <result description="sample string">
                <type type="location">
                </type>
            </result>
        </command>
        
        
    </suite>
    
</dictionary>