run javascript from applescriptobjc code

hi,

i am trying to execute photoshop javascript by clicking button. when i enter the complete path of that javascript file then its working fine. however, when i create package and tried on other machine then it wont work.

now i am trying to run it via path to me but its not working. i have tried following:

tell application id “com.adobe.photoshop”

do javascript file log(path to me as text & “Contents:Resources:myscript.jsx”)

end tell

however its not working.

please help

thanks

You can’t use path to me in AppleScriptObjC. Instead, use NSBundle:

set theBundle to current application's NSBundle's mainBundle()
set thePath to mainBundle's pathForResource_ofType_("my script", "jsx") as text

hi shane,

thanks for your reply

i am sorry but i am new to this platform how would i use mypath with do javascript

i have tried do javascript file (mypath) but its not working.

thanks

The path will be a POSIX path, so you’ll probably need “do javascript (mypath as POSIX file)”.

thanks shan,

but i think i am doing something wrong, i am attaching full code below:

script myscriptAppDelegate

property parent : missing value
property myPath : missing value

on ButtonClicked_(sennder)

set theBundle to current application’s NSBundle’s mainBundle ()
set thePath to mainBundle’s pathForResource_ofType_(“myscript” , “jsx”) as text

tell application id "com.adobe.photoshop

do javascript (thePath as POSIX file)

end tell

end ButtonClicked_

this appdelegate object is linked to button

please help

thanks

You’ve got lots of mistakes in there…The parent property should be class “NSObject”, sender has one n, no space between mainBundle and (), you’re missing " after the app identifier. Are you copying and pasting or retyping?

i am sry,

my mac pc dont have internet so i a typing. let me correct it

script myscriptAppDelegate

property parent : class “NSObject”
property parent : missing value
property myPath : missing value

on ButtonClicked_(sender)

set theBundle to current application’s NSBundle’s mainBundle()
set thePath to mainBundle’s pathForResource_ofType_(“myscript” , “jsx”) as text

tell application id “com.adobe.photoshop”

do javascript (thePath as POSIX file)

end tell

end ButtonClicked_

sry one more mistake left…

script myscriptAppDelegate

property parent : class “NSObject”
property thePath: missing value
property mainBundle: missing value

on ButtonClicked_(sender)

set theBundle to current application’s NSBundle’s mainBundle()
set thePath to mainBundle’s pathForResource_ofType_(“myscript” , “jsx”) as text

tell application id “com.adobe.photoshop”

do javascript (thePath as POSIX file)

end tell

end ButtonClicked_

OK, it looks like a bug in my earlier code too. Get rid of the property mainBundle; you don’t want a property named after a method. The next line then says"mainBundle’s pathForResource_ofType_", when it should say “theBundle’s pathForResource_ofType_”. And it looks like the “do javascript” command is a bit fussy, so you need to give it both the word file and the HFS path. So:

	on ButtonClicked_(sender)
		set theBundle to current application's NSBundle's mainBundle()
		set thePath to theBundle's pathForResource_ofType_("myscript", "jsx") as text
        set theHFSPath to (thePath as POSIX file) as text
		tell application id "com.adobe.photoshop"
			do javascript file theHFSPath
		end tell
	end ButtonClicked_

thanks shan,

its working but when i click on button then it give error in photoshop "Error 25: Expected: ;.

Line: 1

→ “script path in resource directory”

thanks again

That’s what I get if I use “do javascript thePath” rather than “do javascript file theHFSPath”. Check your code.

thanks a ton shan…!!!:smiley:

ur great…

can i buy your book as ebook or do i need to buy hardcopy only.

please let me know… i want to buy immediately.

thanks again

It’s ebook only.

Glad you got there in the end!

hi shan,

sorry to bothering you again.

if i want to run more then one script file then how should i manage.

please help

You mean more than one js file? Just use the same code again.

hi shan,

i can run more then one script, however, when i click on the button then it initialize photoshop cs6. i have two photoshop installed that is cs4 and cs6. previously it wasnt happening. however, when i use “set thePath” then it populate cs6 every time i click on button.

pls help

I am using this code

set theBundle to current application’s NSBundle’s mainBundle()
set thePath to theBundle’s pathForResource_ofType_(“FirstPreferences”, “jsx”) as text

    set theBundle to current application's NSBundle's mainBundle()
    set thePath1 to theBundle's pathForResource_ofType_("LightenPhoto", "jsx") as text  
    
    set theBundle to current application's NSBundle's mainBundle()
    set thePath2 to theBundle's pathForResource_ofType_("LastPreferences", "jsx") as text 
    
    tell application id "com.adobe.photoshop"
    
    set theHFSPath to (thePath as POSIX file) as text 
         
        do javascript file 	theHFSPath        
    
    set theHFSPath to (thePath1 as POSIX file) as text
        
        do javascript file 	theHFSPath 
                    
    set theHFSPath to (thePath2 as POSIX file) as text        
                      
        do javascript file 	theHFSPath
               
        end tell

Every time when i run this script through button, it initializes Photoshop CS6. Even Photoshop CS4 is Opened.

First, you only need the line “set theBundle to current application’s NSBundle’s mainBundle()” once.

The problem of multiple files is really an old AppleScript problem. The only real solution is to use full path to the application:

set appPath to (path to applications folder as text) & "Adobe Photoshop CS6:Adobe Photoshop CS6.app"
using terms from application id "com.adobe.photoshop"
tell application appPath
...

But you may find the problem only occurs when developing your application. It might be easier to develop it with CS6 anyhow.

hi shan,

thank you for your reply.

i want to make this application run with all the versions of photoshop that is cs2 to cs6. in that case how will we use this??

thanks again

You can’t. If you want to use whatever version the user has, you can’t control which version is started. When you want to develop it, quit all versions first – when you open your project, it will open the default copy. Use the id as you did earlier, and develop in the default value.

This is not really an AS or ASObjC problem – it’s just the way to OS works.