Question about AppleScript & Xcode

I hope that this is the right section. I’m trying to make a launcher for the Commander Keen games in AppleScript. It works great in Script Editor:

set appLocation to path to me as string

set theCommanderKeenChoices to {"Episode One: Marooned on Mars", "Commander Keen in: Keen Dreams", "Episode IV: Secret of the Oracle", "Commander Keen in: Aliens Ate My Babysitter!"}

set theSelectedCommanderKeen to choose from list theCommanderKeenChoices with title "Commander Keen Collection" with prompt "Select your shareware game"

if theSelectedCommanderKeen contains "Episode One: Marooned on Mars" then	
	set theFile to appLocation & "Contents:Resources:games:keen123:Episode One - Marooned on Mars.app"
	tell application "Finder" to open file theFile

else if theSelectedCommanderKeen contains "Commander Keen in: Keen Dreams" then
	set theFile to appLocation & "Contents:Resources:games:keen3.5:Commander Keen in - Keen Dreams.app"
	tell application "Finder" to open file theFile
	
else if theSelectedCommanderKeen contains "Episode IV: Secret of the Oracle" then
	set theFile to appLocation & "Contents:Resources:games:keen45:Episode IV - Secret of the Oracle.app"
	tell application "Finder" to open file theFile
	
else if theSelectedCommanderKeen contains "Commander Keen in: Aliens Ate My Babysitter!" then
	set theFile to appLocation & "Contents:Resources:games:keen6:Commander Keen in - Aliens Ate My Babysitter!.app"
	tell application "Finder" to open file theFile
	
end if

Here’s the app using the code above. This version uses the freely available Shareware/demo versions of the games.

The problem is when I try to use the same code in my Xcode version of the launcher. Once I try and open an app via the launcher, I get this message in the output box:

Can’t make «class ocid» id «data optr0000000000A5220000600000» into type constant. (error -1700)

This is the code that ends up working in Xcode:

    on buttonPushedKeen1_(sender) 
           
        set theFile to "Macintosh HD:Applications:Commander Keen Collection:Commander Keen Collection.app:Contents:Resources:files:games:keen123:Episode One - Marooned on Mars.app"
        
        tell application "Finder" to open file theFile
        
        return
        
    end buttonPushedKeen1_

Is there any way I can fix the type error so that I can use the original code?

Try using:

tell application “Finder” to open file (theFile as text)

Thanks for the suggestion It didn’t the problem that I memtioned. Here’s the full code of the AppDelegate.applescript file. i uploaded it to pastebin so that the code wouldn’t be taking up most of the page.

https://pastebin.com/xP8Y6iQB

I feel like I’m missing something. Not sure what exactly.

The problem is your first line: you can’t use path to me in an Xcode project. Try:

set appLocation to current application's NSBundle's mainBundle()'s resourcePath() as text
set appLocation to (POSIX file appLocation) as text

Then change this sort of code:

   set theFile to appLocation & "Contents:Resources:games:keen123:Episode One - Marooned on Mars.app"

to:

   set theFile to appLocation & ":games:keen123:Episode One - Marooned on Mars.app"

Thanks for the clarification on that error. Ok, so here’s the code for first button listed:


on buttonPushedKeen1:sender
      
      set appLocation to current application's NSBundle's mainBundle()'s resourcePath() as text
      set appLocation to (POSIX file appLocation) as text
      set theFile to appLocation & ":files:games:keen123:Episode One - Marooned on Mars.app"
      tell application "Finder" to open file (theFile as text)
      
end buttonPushedKeen1_

The output box says this now when I try and press that button after building the app:

Try changing this:

set appLocation to (POSIX file appLocation) as text

to this:

set appLocation to (appLocation as POSIX file) as text

THAT WORKED!! :smiley: Thank you for your help :slight_smile: