Skip to main content
Bedazzled532
Inspiring
July 19, 2024
Answered

Run a script on selected Item

  • July 19, 2024
  • 1 reply
  • 1459 views

I have written a script which converts some letters. 

The script itself works fine. But when I added code to run on the selected matter only, the scritpt is giving error "sel.changeGrep is not a function".

 

If I do not select anything the script should run on whole document. If I select and run the script, it should run on selection only.

 

What am I doing wrong?

 

Here is the code:

function main() {
    
	var sel= app.selection;
	urdu2Arabic(sel);
}

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Urdu to Arabic');

function urdu2Arabic(sel) {
	
	app.findGrepPreferences=app.changeGrepPreferences=null;
	app.findGrepPreferences.findWhat="\\x{6c1}";
	app.changeGrepPreferences.changeTo = "\\x{647}";
	if(!sel){
		app.activeDocument.changeGrep();	
	}
	else{
		sel.changeGrep();
	}
	app.findGrepPreferences=app.changeGrepPreferences=null;
	app.findGrepPreferences.findWhat="\\x{6c3}";
	app.changeGrepPreferences.changeTo = "\\x{629}";
		if(!sel){
		app.activeDocument.changeGrep();	
	}
	else{
		sel.changeGrep();
	}

	// app.findGrepPreferences=app.changeGrepPreferences=null;
	// app.findGrepPreferences.findWhat="\\x{6be}";
	// app.changeGrepPreferences.changeTo = "\\x{647}";
	// app.activeDocument.changeGrep();

	// app.findGrepPreferences=app.changeGrepPreferences=null;
	// app.findGrepPreferences.findWhat="\\x{6a9}";
	// app.changeGrepPreferences.changeTo = "\\x{643}";
	// app.activeDocument.changeGrep();

	// app.findGrepPreferences=app.changeGrepPreferences=null;
	// app.findGrepPreferences.findWhat="\\x{6cc}";
	// app.changeGrepPreferences.changeTo = "\\x{64a}";
	// app.activeDocument.changeGrep();

	// app.findGrepPreferences=app.changeGrepPreferences=null;
	// app.findGrepPreferences.findWhat="\\x{627}\\x{654}";
	// app.changeGrepPreferences.changeTo = "\\x{623}";
	// app.activeDocument.changeGrep();

	// app.findGrepPreferences=app.changeGrepPreferences=null;
	// app.findGrepPreferences.findWhat="\\x{627}\\x{655}";
	// app.changeGrepPreferences.changeTo = "\\x{625}";
	// app.activeDocument.changeGrep();

	// app.findGrepPreferences=app.changeGrepPreferences=null;
	// app.findGrepPreferences.findWhat="\\x{6CC}\\x{654}";
	// app.changeGrepPreferences.changeTo = "\\x{626}";
	// app.activeDocument.changeGrep();

	// app.findGrepPreferences=app.changeGrepPreferences=null;
	// app.findGrepPreferences.findWhat="\\x{64A}\\x{654}";
	// app.changeGrepPreferences.changeTo = "\\x{626}";
	// app.activeDocument.changeGrep();

	// app.findGrepPreferences=app.changeGrepPreferences=null;
	// app.findGrepPreferences.findWhat="\\x{648}\\x{654}";
	// app.changeGrepPreferences.changeTo = "\\x{624}";
	// app.activeDocument.changeGrep();
	
	app.findGrepPreferences=app.changeGrepPreferences=null;
}

 

I have commented some parts of the script for testing purpose only.

 

Thanks.

This topic has been closed for replies.
Correct answer Robert at ID-Tasker

Also, doesn’t seem like you need the extra function? This simplifies and tests for a text selection as @brian_p_dts suggested:

 

app.doScript(urdu2Arabic, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Urdu to Arabic');

function urdu2Arabic() {
	var sel= app.selection;
	app.findGrepPreferences=app.changeGrepPreferences=null;
	app.findGrepPreferences.findWhat="\\x{6c1}";
	app.changeGrepPreferences.changeTo = "\\x{647}";
	if(sel.length == 1 && sel[0].hasOwnProperty("changeGrep")){
        sel[0].changeGrep();	
	}
	else{
		app.activeDocument.changeGrep();
	}
	app.findGrepPreferences=app.changeGrepPreferences=null;
	app.findGrepPreferences.findWhat="\\x{6c3}";
	app.changeGrepPreferences.changeTo = "\\x{629}";
	if(sel.length == 1 && sel[0].hasOwnProperty("changeGrep")){
        sel[0].changeGrep();
	}
	else{
		app.activeDocument.changeGrep();
	}
	app.findGrepPreferences=app.changeGrepPreferences=null;
}

@Bedazzled532

 

You can optimise your code a bit - instead of checking what is selected after setting GREP options every time:

 

 

if (sel.length == 1 && sel[0].hasOwnProperty("changeGrep"))
{
   sel[0].changeGrep();
}
else
{	 
   app.activeDocument.changeGrep();
}

 

 

You could set it once - at the start - and pass as an argument:

 

 

if (sel.length == 1 && sel[0].hasOwnProperty("changeGrep"))
{
   WhatToProcess = sel[0];
}
else
{	 
   WhatToProcess = app.activeDocument;
}

 

 

So your code would look like this:

 

 

function main() {
	var sel= app.selection;
	var WhatToProcess = '';
	if (sel.length == 1 && sel[0].hasOwnProperty("changeGrep"))
	{
	   WhatToProcess = sel[0];
	}
	else
	{	 
	   WhatToProcess = app.activeDocument;
	}
	urdu2Arabic(WhatToProcess);
}

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Urdu to Arabic');


function urdu2Arabic(WhatToProcess) {	
	app.findGrepPreferences=app.changeGrepPreferences=null;
	app.findGrepPreferences.findWhat="\\x{6c1}";
	app.changeGrepPreferences.changeTo = "\\x{647}";
	try{
		WhatToProcess.changeGrep();
	}catch(e){exit();}
	
	app.findGrepPreferences=app.changeGrepPreferences=null;
	app.findGrepPreferences.findWhat="\\x{6c3}";
	app.changeGrepPreferences.changeTo = "\\x{629}";
	try{
		WhatToProcess.changeGrep();
	}catch(e){exit();}

	app.findGrepPreferences=app.changeGrepPreferences=null;
	app.findGrepPreferences.findWhat="\\x{6be}";
	app.changeGrepPreferences.changeTo = "\\x{647}";
	try{
		WhatToProcess.changeGrep();
	}catch(e){exit();}

//.....
}

 

 

1 reply

brian_p_dts
Community Expert
Community Expert
July 19, 2024

Selection is an array. So you need sel[0]. You can also gracefully exit with a try/catch, or checking if !sel[0].hasOwnProperty("changeGrep"). You'd probably also want to check sel.lengh, constructor types, etc.

Bedazzled532
Inspiring
July 19, 2024

Thanks for the reply.

It now works only on the selected items and not the whole document if I do not select anything.

 

I tried changing this code:

if(!sel){
		app.activeDocument.changeGrep();	
	}

To this but it did not work.

if(!sel[]0){
		app.activeDocument.changeGrep();	
	}

 

Here is the udpated code:

function main() {
    
	var sel= app.selection;
	urdu2Arabic(sel);
}

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Urdu to Arabic');


function urdu2Arabic(sel) {	
	app.findGrepPreferences=app.changeGrepPreferences=null;
	app.findGrepPreferences.findWhat="\\x{6c1}";
	app.changeGrepPreferences.changeTo = "\\x{647}";
	if(!sel){
		app.activeDocument.changeGrep();	
	}
	else{
		try{
			sel[0].changeGrep();
		}catch(e){exit();}
	}
	
	app.findGrepPreferences=app.changeGrepPreferences=null;
	app.findGrepPreferences.findWhat="\\x{6c3}";
	app.changeGrepPreferences.changeTo = "\\x{629}";
		if(!sel){
		app.activeDocument.changeGrep();	
	}
	else{
		try{
			sel[0].changeGrep();
		}catch(e){exit();}
	}

	app.findGrepPreferences=app.changeGrepPreferences=null;
	app.findGrepPreferences.findWhat="\\x{6be}";
	app.changeGrepPreferences.changeTo = "\\x{647}";
	if(!sel){
		app.activeDocument.changeGrep();	
	}
	else{
		try{
			sel[0].changeGrep();
		}catch(e){exit();}
	}

	app.findGrepPreferences=app.changeGrepPreferences=null;
	app.findGrepPreferences.findWhat="\\x{6a9}";
	app.changeGrepPreferences.changeTo = "\\x{643}";
	if(!sel){
		app.activeDocument.changeGrep();	
	}
	else{
		try{
			sel[0].changeGrep();
		}catch(e){exit();}
	}

	app.findGrepPreferences=app.changeGrepPreferences=null;
	app.findGrepPreferences.findWhat="\\x{6cc}";
	app.changeGrepPreferences.changeTo = "\\x{64a}";
	if(!sel){
		app.activeDocument.changeGrep();	
	}
	else{
		try{
			sel[0].changeGrep();
		}catch(e){exit();}
	}

	app.findGrepPreferences=app.changeGrepPreferences=null;
	app.findGrepPreferences.findWhat="\\x{627}\\x{654}";
	app.changeGrepPreferences.changeTo = "\\x{623}";
	if(!sel){
		app.activeDocument.changeGrep();	
	}
	else{
		try{
			sel[0].changeGrep();
		}catch(e){exit();}
	}

	app.findGrepPreferences=app.changeGrepPreferences=null;
	app.findGrepPreferences.findWhat="\\x{627}\\x{655}";
	app.changeGrepPreferences.changeTo = "\\x{625}";
	if(!sel){
		app.activeDocument.changeGrep();	
	}
	else{
		try{
			sel[0].changeGrep();
		}catch(e){exit();}
	}

	app.findGrepPreferences=app.changeGrepPreferences=null;
	app.findGrepPreferences.findWhat="\\x{6CC}\\x{654}";
	app.changeGrepPreferences.changeTo = "\\x{626}";
	if(!sel){
		app.activeDocument.changeGrep();	
	}
	else{
		try{
			sel[0].changeGrep();
		}catch(e){exit();}
	}

	app.findGrepPreferences=app.changeGrepPreferences=null;
	app.findGrepPreferences.findWhat="\\x{64A}\\x{654}";
	app.changeGrepPreferences.changeTo = "\\x{626}";
	if(!sel){
		app.activeDocument.changeGrep();	
	}
	else{
		try{
			sel[0].changeGrep();
		}catch(e){exit();}
	}

	app.findGrepPreferences=app.changeGrepPreferences=null;
	app.findGrepPreferences.findWhat="\\x{648}\\x{654}";
	app.changeGrepPreferences.changeTo = "\\x{624}";
	if(!sel){
		app.activeDocument.changeGrep();	
	}
	else{
		try{
			sel[0].changeGrep();
		}catch(e){exit();}
	}
	
	app.findGrepPreferences=app.changeGrepPreferences=null;
}

 

brian_p_dts
Community Expert
Community Expert
July 19, 2024

Change if(!sel) to if(!sel.length)

 

Selection is always a valid object even if its array is empty, I believe.