Pleeeease Say Yes

Hey guys,

I’m almost afraid to ask this but is it possible to use script objects with AppleScript Studio?

Thanks,
-Rob

Yes

Hello,

Thanks for the reply. So what’s the secret?
When I typ in the command:

tell CommandLib’s makeUserFolder(“rc”,“FGS”,“Preflight”) to run

I keep getting an error:

"Finder got an error: “” doesn’t understand the
makeUserFolder message. (-1708)

Any Idea what I might be doing wrong?

Thanks again,
-Rob

The first thing I’d check is whether you’re executing it from within a Finder tell block. The error tells me that you’ve probably got some code where it ain’t supposed to be…:stuck_out_tongue:

Your script object’s parent is the main script, so the Finder has no idea what to do with your code. Placing code within tell blocks unnecessarily is always a bad idea, so your first goal should be getting the line you posted out of a Finder tell block. If you think it absolutely must be inside the block, then try running it as “my”, so the Finder doesn’t try to figure out what the command means. When I made up a little test script, I got an error trying to use your syntax, so I’d recommend using one of these two…

tell application "Finder"
	--> do some stuff
end tell
tell CommandLib to makeUserFolder("rc", "FGS", "Preflight")

-- OR --
tell application "Finder"
	--> do some stuff
	tell my CommandLib to makeUserFolder("rc", "FGS", "Preflight")
end tell

j

Yes, but did you first import a file containing my subroutine. Just to make sure we’re not talking past each other, If you have a library of frequently used subroutines, AppleScript gives you the ablility to call those subroutines from a separate script file by using the “load script” command. In my case, I have a script file in my Documents folder named, “ScriptLib.scpt”. I load it with the command:

set scriptPath to ((path to documents folder as string) & “ScriptLib.scpt”)
set CommandLib to load script file scriptPath

The subroutine I’m trying to call is:

on makeUserFolder(tech, theName, thePath)
	script
		tell application "Finder"
			set curDate to do shell script "date +%m%d"
			set folderName to tech & "_" & theName & "_" & curDate
			make new folder in folder thePath with properties {name:folderName}
		end tell
	end script
end makeUserFolder

When I attempt to call the subroutine…

tell commandLib's makeUserFolder("rc", "FGS", path to desktop as string) to run

…I get the error. It works like a charm in AppleScript, but in Studio it’s no go.

Is this more clear?

Thanks again,
-Rob

No, because you didn’t tell me that’s what you were doing. You have to give up all the information, because I haven’t got my mindreaders license yet. :stuck_out_tongue:

I created a script file, and loaded it using the applescript code below, and it worked fine for me. No errors, and a folder was created on my desktop as expected.

Applescript code:

set scriptPath to ((path to documents folder as string) & "ScriptLib.scpt")
set CommandLib to load script file scriptPath
tell CommandLib's makeUserFolder("rc", "FGS", path to desktop as string) to run

Contents of ScriptLib.scpt:

on makeUserFolder(tech, theName, thePath)
	script
		tell application "Finder"
			set curDate to do shell script "date +%m%d"
			set folderName to tech & "_" & theName & "_" & curDate
			make new folder in folder thePath with properties {name:folderName}
		end tell
	end script
end makeUserFolder

The error you got clearly indicates that the Finder is trying to execute the script object, not your main script. The code above shows that the “makeUserFolder” routine contains the Finder tell block. Something in your script must be off, because the only way that the Finder should know about the “makeUserFolder” routine is if it’s somehow (or somewhere else) calling the routine from within a tell block.

j

May I ask why your subroutine is returning a script object? This code seems to have the same effect:

script CommandLib
	on makeUserFolder(tech, theName, thePath)
		tell application "Finder"
			set curDate to do shell script "date +%m%d"
			set folderName to tech & "_" & theName & "_" & curDate
			make new folder in folder thePath with properties {name:folderName}
		end tell
	end makeUserFolder
end script

tell CommandLib to makeUserFolder("rc", "FGS", path to desktop as string)

j,

My apologies for being so vague. After your first post I went back to make sure the command wasn’t happening inside any tell blocks and sure enough it was. So I moved it outside of the tell block and got a different but similar error:

AppleScript error
“” doesn’t understand the makeUserFolder message.
(-1708)

Bruce,

Your solution works great if the script object, and the command to run it, are in the same script. What I’m attempting is, to have the subroutine housed in a separate scirpt file and then call it from within my A.S.S. application.

Thanks,
-Rob

I assume you’re using the “load script” command.

Using load script returns a script object.

If you had a script file that contained this.

on makeUserFolder(tech, theName, thePath)
	tell application "Finder"
		set curDate to do shell script "date +%m%d"
		set folderName to tech & "_" & theName & "_" & curDate
		make new folder in folder thePath with properties {name:folderName}
	end tell
end makeUserFolder

.and you used some code like this.

set myScriptObject to load script alias ((path to scripts folder as Unicode text) & "Lib:MySubs.scpt")

.then you should end up with a script object (“myScriptObject”) which contains a subroutine (“makeUserFolder”). That would be the same as this:

script myScriptObject
	on makeUserFolder(tech, theName, thePath)
		tell application "Finder"
			set curDate to do shell script "date +%m%d"
			set folderName to tech & "_" & theName & "_" & curDate
			make new folder in folder thePath with properties {name:folderName}
		end tell
	end makeUserFolder
end script

Regardless of how this script object was created, you could call the subroutine using something like this:

tell myScriptObject to makeUserFolder("hello", "world", path to desktop as Unicode text)

At this point, the tell block inside that subroutine is run.

However, with your script, the subroutine does not run the code. Instead, it creates another script object, which is returned as the result of the subroutine, and then the contents of that new script object are executed.

Bruce,

I followed your instructions to the letter and I get the error:

“” doesn’t understand the <> message.
(-1708)

I tried it in AppleScript, and it worked fine. I don’t understand, it works great in AppleScript but refuses to work in Studio.

Thanks again,
-Rob

Hmm. when searching for that event, I found this: http://lists.apple.com/archives/applescript-users/2001/Aug/msg01228.html

Hi,

You’re trying to gain access to your script library from a subroutine, so its scope needs to be global. Here’s a simple example where I have a window with a button.

global ScriptLib

on awake from nib theObject
set ScriptLib to load script file “Macintosh HD:Users:kel:Desktop:ScriptLib”
end awake from nib

on clicked theObject
set newScript to MakeScript(“hello”) of ScriptLib
run script newScript
tell newScript
set p to the_text of it
end tell
log p
end clicked

Here’s the compiled script, that is located on the desktop:

on MakeScript(t)
script
property the_text : t
display dialog the_text
end script
end MakeScript

Note that to access newScript from outside the clicked handler, that needs to be global also.

If I’m way off on this, then disregard, but the main thing is the scope of the variables.

gl,

Another way to make the script lib global is load it as a property:

property ScriptLib : load script file “Macintosh HD:Users:kel:Desktop:ScriptLib”

on awake from nib theObject
end awake from nib

on clicked theObject
set newScript to MakeScript(“hello”) of ScriptLib
run script newScript
tell newScript
set p to the_text of it
end tell
log p
end clicked

gl,

Thank you to everyone who contributed to this discussion. Your input was greatly appreciated.

-rc