trying to load a script alias from within an application bundle

Hello all,

I am trying to create an application bundle in which the main script calls upon two other scripts throughout it’s run cycle. In the past, I’ve been able to load items (although not scripts) like this:

--figure out where the beep sound is located
set beep_sound to (path to me) & "Contents:Resources:Sounds:beep.mov" as string
set beep_sound to beep_sound as alias

Since that worked out so well, I tried to load a script this way:

MAIN SCRIPT


set theScript to load script alias (path to me) & "Contents:Resources:Scripts:script1.scpt" as string
set theScript to theScript as alias

-- a bunch of stuff goes here, then later we have

set theScript to load script alias (path to me) & "Contents:Resources:Scripts:script2.scpt" as string
set theScript to theScript as alias

When I run the launch the application bundle (called “test_application”), I get the following error:

Can’t get alias (alias Macintosh HD:Users:huber:Desktop:test_application.app:").

When I throw some more parenthesis into the mix (out of desperation & ignorance), I get a similar error (can’t MAKE alias instead of can’t GET alias)

MAIN SCRIPT


set theScript to load script alias ((path to me) & "Contents:Resources:Scripts:script1.scpt") as string
set theScript to theScript as alias

-- a bunch of stuff goes here, then later we have

set theScript to load script alias ((path to me) & "Contents:Resources:Scripts:script2.scpt") as string
set theScript to theScript as alias

ERROR
Can’t make alias {alias Macintosh HD:Users:huber:Desktop:test_application.app:",“Contents:Resources:Scripts:script1.scpt”} of <> into type string.

Any advice?

Thanks as always,

Dave

Hi Dave,


set theScript to (path to me) & "Contents:Resources:Sounds:script1.scpt" as string

This code has your script object in it. From here you can call it or the handlers, scripts
within it.

eg.


run theScript --runs the script

--if you had a handler called this_beep with an argument for how many beeps to perform
theScript's this_beep(2)

Cheers!

Craig

try this

set Thescript to load script ((path to me) & "Contents:Resources:Scripts:script1.scpt" as string) as alias

run script Thescript
-- a bunch of stuff goes here, then later we have

set Thescript to load script ((path to me) & "Contents:Resources:Scripts:script2.scpt" as string) as alias

run script Thescript

Also I notice, once I add the scripts to and save the bundle.

I can test the script by running it in Script Editor.
I’m sure that in tiger running Path to me always went to the Path of Script Editor.
But now Path to me goes the saved app bundle :slight_smile:

Can anyone confirm this is new behaviour?

*edit
Works for saved scpt scripts also.

Hi,

just a note:
the code style works, but it’s not very good


set beep_sound to (path to me) & "Contents:Resources:Sounds:beep.mov" as string
set beep_sound to beep_sound as alias

To concatenate a string from several classes I would recommend to do the (explicit) coercion to the requested class at the beginning,
because AppleScript always takes the class of the first element for its (implicit) coercions.


set beep_sound to (path to me as text) & "Contents:Resources:Sounds:beep.mov"

then the prefixed alias works in all cases (if the alias exists)


set beep_sound to alias ((path to me as text) & "Contents:Resources:Sounds:beep.mov")

Thanks, guys. It looks like I am able to load the script like this:

set theScript to ((path to me) & "Contents:Resources:Scripts:script1.scpt")
set theScript to theScript as string
load script alias theScript

Unfortunately, I have a ton of other problems, so back to the drawing board!

Thanks again,

Dave

Hello,

I was trying nearly the same as Dave: launching a script from within a script bundle:


-- Main script
set theScript to ((path to me) & "Contents:Resources:Scripts:beeper.scptd")
set theScript to theScript as string
load script alias theScript

where beeper does a simple beep.
All I get when running in applescript editor is an error about not finding the file
Macintosh:Applications:AppleScript:Script Editor.app:Contents:Resources:Scripts:beeper.scptd

Obviously Applescript editor takes his path as reference. How can I save my main script as an application bundle instead of a script bundle (thus bypassing the Applescript editor pb)?

I work on Tiger 10.4.11, Applescript editor 2.1.1, Applescript 1.10.7

Thanks

– JeremY

Model: PB G4
AppleScript: 1.10.7
Browser: Safari 525.13
Operating System: Mac OS X (10.4)

Hi,

if you run a script in Script Editor of Tiger, path to me refers to the Script Editor application, not to the script (bundle).
Running the script from script menu the reference is correct.

please read post #4 :wink:

StefanK actually helped me with a simple beep program before. In my beep program, I wanted the beep to occur at random times between 10 and 90 seconds. My solution was to use QuickTime to play a sound that was stored within the application. Here’s how it looked:

The beep sound was located inside the app;
Random_Beep_Generator.app/Contents/Resources/Sounds/beep.mov

--Random Beep Generator by Dave Huber - Nov. 2006
--Makes a beep sound randomly between 10 and 90 seconds


--figure out where the beep sound is located
set beep_sound to (path to me) & "Contents:Resources:Sounds:beep.mov" as string
set beep_sound to beep_sound as alias


--Give user a start button
set dialogResponse to the button returned of (display dialog "Welcome to the Beep Randomizer!

Press Start to begin beeping randomly between 10 and 90 seconds." buttons {"Start"} default button 1)


--Launch QT Player, open beep sound
tell application "QuickTime Player"
	activate
	open beep_sound as alias
end tell


--Hide QT player
tell application "System Events" to set visible of (first process whose name begins with "QuickTime") to false

-- Randomize pause time, then Play the beep sound
repeat
	set pause_time to random number from 10 to 90
	tell application "QuickTime Player"
		play document 1
	end tell
	
	--Give user a stop button.  If it isn't pressed, repeat the randomizing & playing function
	tell application "SystemUIServer"
		activate
		
		set StopButton to the button returned of (display dialog "Do you want to stop this infernal beeping yet?" buttons {"STOP!"} default button 1 giving up after pause_time)
		
		if StopButton = "STOP!" then exit repeat
		
	end tell
end repeat

--when the stop button is pressed, quit QT Player and end script
tell application "QuickTime Player"
	quit
end tell

Hope his helps,

-Dave