Target one Application in a Script

Ok,
Here is my request, but more so a quest.

I have two scripts that perform what they are supposed to do. Yet one script is targeted towards an open Indd document and the other is targeted towards an open Illustrator document.

Is there a way to set a script parameter that understands what the top most application is, then only run that portion of the script?

Such a behavior is necessary because this script will be used within a “Quickey” that will involve other actions - I wanted the Quickey to be a universal pulldown that would work for both applications. (this is step one of the Quickey)

Can this be done using these two scripts below?
If so, it would be fantastic!!!

Thanks,
Jeff

tell application "Illustrator CS"
	
	export document 1 to file "Illustrations:Users:admin:Desktop:Logos:Exported_Image.jpg" as JPEG with options {class:JPEG export options, quality:100, blur:0, horizontal scaling:100, vertical scaling:100, matte:false, optimization:false, artboard clipping:false, antialiasing:true}
	
end tell

tell application "InDesign CS"
	tell JPEG export preferences
		set properties to {Exporting Selection:true, JPEG Quality:high, JPEG Rendering style:baseline encoding}
	end tell
	export active document format JPG to file "Illustrations:Users:admin:Desktop:Logos:Exported_Image.jpg"
	
end tell

This was posted by Jacques some time ago - I’ve set it up to run from the Script Editor which is obviously frontmost when you click “Run”. If you run it as a compiled App, put it’s name in the “set visible” line:

tell application "Finder"
	set visible of process "Script Editor" to false -- don't use name extension
	set Name_App to item 1 of (get name of processes whose frontmost is true)
end tell

Now you can use Name_App in an if statement to choose which of the parts of your script to run.

Adam,
I am having trouble getting this to work. Despite the fact that I have been doing scripts for a month or so, I don’t know much at all :frowning:
I really don’t know how to set up the if statements. I am trying different things, but no luck.
Can you or anyone give me some advice?
But regardless, thank you for this script.
-jeff

Jeff:

I don’t have ID so I can’t compile this but here’s a quick way to route your requests…


tell application "Finder"
	set Name_App to item 1 of (get name of processes whose frontmost is true)
	if Name_App contains "InDesign" then
	my idProcess()
	else if Name_App contains "Illustrator" then
	my aiProcess()
	end tell
end tell

on aiProcess()
tell application "Illustrator CS"
		export document 1 to file "Illustrations:Users:admin:Desktop:Logos:Exported_Image.jpg" as JPEG with options {class:JPEG export options, quality:100, blur:0, horizontal scaling:100, vertical scaling:100, matte:false, optimization:false, artboard clipping:false, antialiasing:true}
	end tell
	end aiProcess

on idProcess()
tell application "InDesign CS"
	tell JPEG export preferences
		set properties to {Exporting Selection:true, JPEG Quality:high, JPEG Rendering style:baseline encoding}
	end tell
	export active document format JPG to file "Illustrations:Users:admin:Desktop:Logos:Exported_Image.jpg"
	end tell
end idProcess

Jim Neumann
BLUEFROG

I can’t test this because I don’t have these applications. You try it and let me know. If both applications are running, then this will pick the one that was active last.

tell application "Finder"
	set visible of process "Script Editor" to false -- don't use name extension - this assumes you run it from there.
	set Front_App to item 1 of (get name of processes whose frontmost is true)
end tell

if Front_App is "Illustrator CS" then
tell application "Illustrator CS"
		export document 1 to file "Illustrations:Users:admin:Desktop:Logos:Exported_Image.jpg" as JPEG with options {class:JPEG export options, quality:100, blur:0, horizontal scaling:100, vertical scaling:100, matte:false, optimization:false, artboard clipping:false, antialiasing:true}
	end tell
else if Front_App is "InDesign CS" then
tell application "InDesign CS"
	tell JPEG export preferences
		set properties to {Exporting Selection:true, JPEG Quality:high, JPEG Rendering style:baseline encoding}
	end tell
	export active document format JPG to file "Illustrations:Users:admin:Desktop:Logos:Exported_Image.jpg"
end tell
else
display dialog "Neither application is frontmost"
end if

This works perfectly. Thank you so much for your help, and more so your patience.

jeff

One last request,
Is there a way to stick Photoshop CS inside the script, but have it perform nothing- but simply exit the script with no warnings?

Reason for this madness: Since the additional actions of the Quickey work on the Exported Photoshop file(a result of this script), it would be nice if the same Quickey could work for an active PS document but fly by Quickey’s “first step” which happens to be this “script”.

tell application "Finder"
	set visible of process "Script Editor" to false -- don't use name extension - this assumes you run it from there.
	set Front_App to item 1 of (get name of processes whose frontmost is true)
end tell

if Front_App is "Illustrator CS" then
	tell application "Illustrator CS"
		export document 1 to file "Illustrations:Users:admin:Desktop:Valpak.com:Exported_Image.jpg" as JPEG with options {class:JPEG export options, quality:100, blur:0, horizontal scaling:100, vertical scaling:100, matte:false, optimization:false, artboard clipping:false, antialiasing:true}
	end tell
	
	tell application "Adobe Photoshop CS"
		with timeout of 300 seconds
			activate
			open alias "Illustrations:Users:admin:Desktop:Valpak.com:Exported_Image.jpg" showing dialogs never
			--Do Stuff here
		end timeout
	end tell
	
	
else if Front_App is "InDesign CS" then
	tell application "InDesign CS"
		tell JPEG export preferences
			set properties to {Exporting Selection:true, JPEG Quality:high, JPEG Rendering style:baseline encoding}
		end tell
		export active document format JPG to file "Illustrations:Users:admin:Desktop:Logos:Exported_Image.jpg"
	end tell
	
	tell application "Adobe Photoshop CS"
		with timeout of 300 seconds
			activate
			open alias "Illustrations:Users:admin:Desktop:Logos:Exported_Image.jpg" showing dialogs never
			--Do Stuff here
		end timeout
	end tell
	
	
else
	display dialog "Neither application is frontmost"
end if

jeffkr;

I think this is what you want - It tests for Illustrator, does stuff if there, then exits. If Illustrator is not there, it tests for InDesign, does stuff there, then exits. If neither of the above it tests for Photoshop and if it finds it goes not to the next step but to a shorcut stored separately in QK to do it. If none of the above it starts PS and then executes the shortcut. It might need a delay in between. I wish I could check this but can’t. If you always want it to go on to the next step, then put the tell QK in those places.

tell application "Finder"
	set visible of process "Script Editor" to false -- don't use name extension - this assumes you run it from there.
	set Front_App to item 1 of (get name of processes whose frontmost is true)
end tell

if Front_App is "Illustrator CS" then
	tell application "Illustrator CS"
		export document 1 to file "Illustrations:Users:admin:Desktop:Valpak.com:Exported_Image.jpg" as JPEG with options {class:JPEG export options, quality:100, blur:0, horizontal scaling:100, vertical scaling:100, matte:false, optimization:false, artboard clipping:false, antialiasing:true}
	end tell
		
else if Front_App is "InDesign CS" then
	tell application "InDesign CS"
		tell JPEG export preferences
			set properties to {Exporting Selection:true, JPEG Quality:high, JPEG Rendering style:baseline encoding}
		end tell
		export active document format JPG to file "Illustrations:Users:admin:Desktop:Logos:Exported_Image.jpg"
	end tell	
	
else if Front_App is "Adobe Photoshop CS" then
	tell application "QuicKeys" to play shortcut named "WhateverYouCalledIt" -- stored separately
else
tell application "Adobe Photoshop CS" to activate -- start it up
tell application QuicKeys to play shortcut named "WhateverYouCalledIt"
end if

Hi Adam,
I am sorry to bother you,
But actually,
Step 1 of the Quickey is this script.
Step 2 of the Quickey is a pause
Step 3 is a menu selection item in Photoshop
Step 4 is a pause
Step 5 is a menu selection item in Photoshop

Step 2-5 are necessary for all applications.(indd, Ai, PS)
But step 1 is only need for Illustrator and InDesign (getting the jpeg file ready for the other steps in the Quickey)

I think your script will work but I am getting a syntax error on the word “shortcut”.
Does my description make sense.
I am sorry if I didn’t explain this well.
I will be glad to explain it in more detail. After all you are the one kind enough to help me.

-jeff

Adam,
It’s just me again. Well I got this to work. I think the reason for my failure was the fact that I didn’t realize I wasn’t labeling Photoshop correctly. I wasn’t including “Adobe” in the name.
So I stuck this line in my script right before the last else statement:

else if Front_App is “Adobe Photoshop CS” then
tell application “Adobe Photoshop CS”
end tell

This is now bypassing this script, and step 2 of the Quickey will perform.
That line I added is a bit odd looking, but I haven’t seen an issue yet?

Thanks again for all your help.

-jeff

You’ve lost me. If you don’t want the script to do anything with PS, just leave out the whole test for it. I had thought that you wanted to press on if it was open and do something there. I don’t know what to say about the line

tell application "QuicKeys" to play shortcut named "the Shortcut Name in Quotes"

.

It works for me.

Hi Adam,
It is a bit hard to explain this without screen shots. But the AppleScript is embedded inside the Quickey shortcut. Step 1, (the script), is supposed to produce the Photoshop file from an indd or ai file. But if the Photoshop file is there already, it doesn’t need step one of the Quickey to work, but go directly to step 2 in the Quickey.

I believe you solution would work. But I believe your are referring to a separate script pointing to a separate Quickey? I am sorry, I wasn’t thinking that clear.

But regardles, this is working without a hitch now.
I hope you realize, I am not upset or disappointed. Quite the contrary, this script is incredible! I was up to 2 am trying to make it break. It took me a while to realize I had to remove the line of code regarding the “Script Editor” :slight_smile:

Thank you. This is a great script.

-jeff

Yes - that’s only there so you can test it in the Script Editor - If you’re running it from QK, you don’t need it. When I have a line like that, I normally don’t remove it, I just comment it out (two dashes in front of it).

For the script in step 1, this is all you need (plus any other bits you’ve added):

tell application "Finder"
-- set visible of process "Script Editor" to false -- don't use name extension - this assumes you run it from there.
   set Front_App to item 1 of (get name of processes whose frontmost is true)
end tell

if Front_App is "Illustrator CS" then
   tell application "Illustrator CS"
       export document 1 to file "Illustrations:Users:admin:Desktop:Valpak.com:Exported_Image.jpg" as JPEG with options {class:JPEG export options, quality:100, blur:0, horizontal scaling:100, vertical scaling:100, matte:false, optimization:false, artboard clipping:false, antialiasing:true}
   end tell -- this ends the script and QK goes on to step 2
       
else if Front_App is "InDesign CS" then
   tell application "InDesign CS"
       tell JPEG export preferences
           set properties to {Exporting Selection:true, JPEG Quality:high, JPEG Rendering style:baseline encoding}
       end tell
       export active document format JPG to file "Illustrations:Users:admin:Desktop:Logos:Exported_Image.jpg"
   end tell -- this ends the script and QK goes on to step 2 

else
   tell application "Adobe Photoshop CS" to activate -- start it up. This ends the script and QK goes on to step 2

end if

What you have to know about an “if” statement is that only one of the options is executed, then the script continues after “end if”. If step 2 is PS stuff, then QK will jump there as soon as any option (or no option) passes muster. In step 2, then, you look for the file and process it in PS.

Sorry for the long delay, (held up at work). Adam, this is working flawlessly. I can’t believe you hung in there this long!!!
I owe you a great deal of thanks. More importantly, I appreciate the fact that you explained the if statement in better detail. Every bit helps in trying to grasp this cryptic coding :slight_smile:

Thanks again.
-jeff

You’re welcome. One other word about lines like the second one here:

tell application "Finder"
    set visible of process "Script Editor" to false -- don't use name extension - this assumes you run it from there.
    set Front_App to item 1 of (get name of processes whose frontmost is true)
end tell

As you discovered, if the Script Editor is not running this “throws” an error (throws being the term usually used). There are several ways to avoid this. First, you could check whether the application is frontmost as you do for the other apps and set its visible to false only if it is there, or second, you can use a construct like this:

-- Try allows the script to attempt the action but proceed with the script if the action fails for some reason. To do something else if the action fails, perhaps just a 'display dialog "it borked"', you put it under "on error".

tell application "Finder"
	try
		set visible of process "Whatever" to false
	on error
		-- do something (or nothing if you leave "on error" out).
	end try
end tell

As you progress as a scripter, be sure to visit ScriptBuilders, which is full of ready to go scripts to do stuff in PS, InD, and Illus. Look at some of the other sections in the menu at the top right of the thread or under “Sections¬” on the left of the date bar.

Adam,
You are a tremendous help. And I will most definitely visit “ScriptBuilders”. “Full of ready to use scripts” “ What could be better!
Thanks again for going above and beyond with your help and advice.
Happy Holidays.

jeff