Highlighted

Find Replace Inside Plugin

Explorer ,
Aug 05, 2020

Copy link to clipboard

Copied

Hi Everyone,

I have called a script file inside a plugin but the exact contents doesn't get replaced.

It shows zero replacement 's made for all the time.. I can't find the problem in my code it seems correct..

then what's the problem?????? the code i've used is

if (RadioButton2->IsSelected() == kTrue)
{
CAlert::InformationAlert("Exact");
InterfacePtr<IScriptManager> scriptManager(Utils<IScriptUtils>()->QueryScriptManager(kJavaScriptMgrBoss));
InterfacePtr<IScriptRunner> scriptRunner(scriptManager, UseDefaultIID());
PMString getSearchText = searchTextData->GetString();
PMString getReplaceText = replaceTextData->GetString();
Utils<IScriptArgs>()->Set("searchText", getSearchText);
Utils<IScriptArgs>()->Set("replaceText", getReplaceText);
PMString scriptCode1;
scriptCode1.Append("var searchText= app.scriptArgs.get(\"searchText\");");
scriptCode1.Append("var replaceText= app.scriptArgs.get(\"replaceText\");");
scriptCode1.Append("app.findGrepPreferences.findWhat = '\\b' + searchText + '\\b';");
scriptCode1.Append("var list = app.activeDocument.findGrep();");
scriptCode1.Append("app.findGrepPreferences.findWhat = '\\b' + searchText + '\\b';");
scriptCode1.Append("app.changeGrepPreferences.changeTo = replaceText;");
scriptCode1.Append("app.activeDocument.changeGrep();");
scriptCode1.Append("alert(list.length + \" replacement's made\");");
PMString engineName("myengine");
int32 errorCode = Utils<IExtendScriptUtils>()->RunScriptInEngine(engineName, scriptCode1);
}

Guide me to find the problem....

 

Regards,

-Jothi.

 

I think you may be running into a problem with your backslash escapes, as in scriptCode1.Append("app.findGrepPreferences.findWhat = '\\b' …

the C++ compiler will eat the "\\" and produce a "\".

The JS parser will combine it with the following e.g. b and produce a - don't know, likely just pass the b.

Therefor, no backslash left for the actual grep.

Topics

Scripting, SDK

Views

114

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Find Replace Inside Plugin

Explorer ,
Aug 05, 2020

Copy link to clipboard

Copied

Hi Everyone,

I have called a script file inside a plugin but the exact contents doesn't get replaced.

It shows zero replacement 's made for all the time.. I can't find the problem in my code it seems correct..

then what's the problem?????? the code i've used is

if (RadioButton2->IsSelected() == kTrue)
{
CAlert::InformationAlert("Exact");
InterfacePtr<IScriptManager> scriptManager(Utils<IScriptUtils>()->QueryScriptManager(kJavaScriptMgrBoss));
InterfacePtr<IScriptRunner> scriptRunner(scriptManager, UseDefaultIID());
PMString getSearchText = searchTextData->GetString();
PMString getReplaceText = replaceTextData->GetString();
Utils<IScriptArgs>()->Set("searchText", getSearchText);
Utils<IScriptArgs>()->Set("replaceText", getReplaceText);
PMString scriptCode1;
scriptCode1.Append("var searchText= app.scriptArgs.get(\"searchText\");");
scriptCode1.Append("var replaceText= app.scriptArgs.get(\"replaceText\");");
scriptCode1.Append("app.findGrepPreferences.findWhat = '\\b' + searchText + '\\b';");
scriptCode1.Append("var list = app.activeDocument.findGrep();");
scriptCode1.Append("app.findGrepPreferences.findWhat = '\\b' + searchText + '\\b';");
scriptCode1.Append("app.changeGrepPreferences.changeTo = replaceText;");
scriptCode1.Append("app.activeDocument.changeGrep();");
scriptCode1.Append("alert(list.length + \" replacement's made\");");
PMString engineName("myengine");
int32 errorCode = Utils<IExtendScriptUtils>()->RunScriptInEngine(engineName, scriptCode1);
}

Guide me to find the problem....

 

Regards,

-Jothi.

 

I think you may be running into a problem with your backslash escapes, as in scriptCode1.Append("app.findGrepPreferences.findWhat = '\\b' …

the C++ compiler will eat the "\\" and produce a "\".

The JS parser will combine it with the following e.g. b and produce a - don't know, likely just pass the b.

Therefor, no backslash left for the actual grep.

Topics

Scripting, SDK

Views

115

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Aug 05, 2020

Copy link to clipboard

Copied

This is the scripting forum, you want to go to the SDK forum.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Aug 05, 2020

Copy link to clipboard

Copied

Peter, there are no separate forums now, we just have a single forum with multiple tags like SDK, Scripting etc. Not the ideal setup but this is what we got.

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Aug 05, 2020

Copy link to clipboard

Copied

Oh, didn't know that, thanks. Not a good set-up indeed.

P.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Explorer ,
Aug 07, 2020

Copy link to clipboard

Copied

Hi Expects,

Is their is any solution for my query?????

 

Regards,

-Jothi.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Aug 07, 2020

Copy link to clipboard

Copied

Hi Jothi,

does the ExtendScript part work if you execute that code from an ExtendScript file of InDesign's Scripts folder?

There could be various conditions in your sample InDesign document where your code does nothing.

 

The most important thing you do not show:

What exactly is in the searchText variable and what is in the replaceText variable?

 

Hm. And why do you repeat two lines of your code?

 

To debug this we need a sample InDesign document and valid contents of the two mentioned variables.

 

Regards,
Uwe Laubender

( ACP )

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Explorer ,
Aug 10, 2020

Copy link to clipboard

Copied

Yes! Dirk, While executing the code from an ExtendScript file of InDesign's Scripts folder it runs good...

While using the same scripting inside plugin SDK it won't work...

I think that the problem occur bcz of the special characters used inside plugin SDK..

I've attached my code to know better about it....

For your information Contains with alone is working good 


void PicIcoRollOverButtonObserver::Update
(
const ClassID& theChange,
ISubject* theSubject,
const PMIID &protocol,
void* changedBy
)
{
do {
if (theChange == kTrueStateMessage)
{
// No behaviour in this sample: but indicate at least that the message has been received.
//CAlert::InformationAlert(kPicIcoMessageKey);
InterfacePtr<IControlView> view(this, UseDefaultIID());

WidgetID widgetid = view->GetWidgetID();
InterfacePtr<IScriptManager> scriptManager(Utils<IScriptUtils>()->QueryScriptManager(kJavaScriptMgrBoss));
InterfacePtr<IScriptRunner> scriptRunner(scriptManager, UseDefaultIID());
IDocument* document = Utils<ILayoutUIUtils>()->GetFrontDocument();
if (document == nil)
{
CAlert::InformationAlert("No documents are opened! Please open a document and try again");
}
if (document != nil)
{
InterfacePtr<IWidgetParent> myParent(this, UseDefaultIID());
InterfacePtr<IPanelControlData> panel((IPanelControlData*)myParent->QueryParentFor(IID_IPANELCONTROLDATA));
IControlView* SearchEditTextBoxView = panel->FindWidget(kSearchTextEditBoxWidgetID);
InterfacePtr<ITextControlData> searchTextData(SearchEditTextBoxView, UseDefaultIID());
IControlView* ReplaceEditTextBoxView = panel->FindWidget(kReplaceTextEditBoxWidgetID);
InterfacePtr<ITextControlData> replaceTextData(ReplaceEditTextBoxView, UseDefaultIID());
IControlView* CheckBoxView = panel->FindWidget(kCaseSensitiveCheckBoxWidgetID);
InterfacePtr<ITriStateControlData> CheckBox1(CheckBoxView, UseDefaultIID());
IControlView* RadioButtonView1 = panel->FindWidget(kContainsWithRadioWidgetID);
InterfacePtr<ITriStateControlData> RadioButton1(RadioButtonView1, UseDefaultIID());
IControlView* RadioButtonView2 = panel->FindWidget(kExactRadioWidgetID);
InterfacePtr<ITriStateControlData> RadioButton2(RadioButtonView2, UseDefaultIID());
IControlView* RadioButtonView3 = panel->FindWidget(kStartWithRadioWidgetID);
InterfacePtr<ITriStateControlData> RadioButton3(RadioButtonView3, UseDefaultIID());
IControlView* RadioButtonView4 = panel->FindWidget(kEndWithRadioWidgetID);
InterfacePtr<ITriStateControlData> RadioButton4(RadioButtonView4, UseDefaultIID());
if (widgetid == ReplaceButtonIconWidgetID)
{
if (searchTextData)
{
PMString searchText;
searchText = searchTextData->GetString();

if (searchText.IsEmpty())
{
CAlert::InformationAlert("Please enter the search text");
break;
}
if (replaceTextData)
{
PMString replaceText;
replaceText = replaceTextData->GetString();

if (replaceText.IsEmpty())
{
CAlert::InformationAlert("Please enter the replace text");
break;
}
if (CheckBox1->IsSelected() == kTrue)
{
if (RadioButton1->IsSelected() == kTrue)
{
CAlert::InformationAlert("Case Sensitive Contains with");
InterfacePtr<IScriptManager> scriptManager(Utils<IScriptUtils>()->QueryScriptManager(kJavaScriptMgrBoss));
InterfacePtr<IScriptRunner> scriptRunner(scriptManager, UseDefaultIID());
PMString getSearchText = searchTextData->GetString();
PMString getReplaceText = replaceTextData->GetString();
Utils<IScriptArgs>()->Set("searchText", getSearchText);
Utils<IScriptArgs>()->Set("replaceText", getReplaceText);
PMString scriptCode;
scriptCode.Append("var searchText= app.scriptArgs.get(\"searchText\");");
scriptCode.Append("var replaceText= app.scriptArgs.get(\"replaceText\");");
scriptCode.Append("app.findGrepPreferences = NothingEnum.NOTHING;");
scriptCode.Append("app.findGrepPreferences.findWhat = searchText;");
scriptCode.Append("var list = app.activeDocument.findGrep();");
scriptCode.Append("app.findGrepPreferences.findWhat = searchText;");
scriptCode.Append("app.changeGrepPreferences.changeTo = replaceText;");
scriptCode.Append("app.activeDocument.changeGrep();");
scriptCode.Append("app.findGrepPreferences = NothingEnum.NOTHING;");
scriptCode.Append("alert(list.length + \" replacement's made\");");
PMString engineName("myengine");
int32 errorCode = Utils<IExtendScriptUtils>()->RunScriptInEngine(engineName, scriptCode);
}
if (RadioButton2->IsSelected() == kTrue)
{
CAlert::InformationAlert("Case Sensitive Exact");
InterfacePtr<IScriptManager> scriptManager(Utils<IScriptUtils>()->QueryScriptManager(kJavaScriptMgrBoss));
InterfacePtr<IScriptRunner> scriptRunner(scriptManager, UseDefaultIID());
PMString getSearchText = searchTextData->GetString();
PMString getReplaceText = replaceTextData->GetString();
Utils<IScriptArgs>()->Set("searchText", getSearchText);
Utils<IScriptArgs>()->Set("replaceText", getReplaceText);
PMString scriptCode1;
scriptCode1.Append("var searchText= app.scriptArgs.get(\"searchText\");");
scriptCode1.Append("var replaceText= app.scriptArgs.get(\"replaceText\");");
scriptCode1.Append("app.findGrepPreferences = NothingEnum.NOTHING;");
scriptCode1.Append("app.findGrepPreferences.findWhat = '\\b' + searchText + '\\b';");
scriptCode1.Append("var list = app.activeDocument.findGrep();");
scriptCode1.Append("app.findGrepPreferences.findWhat = '\\b' + searchText + '\\b';");
scriptCode1.Append("app.changeGrepPreferences.changeTo = replaceText;");
scriptCode1.Append("app.activeDocument.changeGrep();");
scriptCode1.Append("app.findGrepPreferences = NothingEnum.NOTHING;");
scriptCode1.Append("alert(list.length + \" replacement's made\");");
PMString engineName("myengine");
int32 errorCode = Utils<IExtendScriptUtils>()->RunScriptInEngine(engineName, scriptCode1);
}
if (RadioButton3->IsSelected() == kTrue)
{
CAlert::InformationAlert("Case Sensitive Starts with");
InterfacePtr<IScriptManager> scriptManager(Utils<IScriptUtils>()->QueryScriptManager(kJavaScriptMgrBoss));
InterfacePtr<IScriptRunner> scriptRunner(scriptManager, UseDefaultIID());
PMString getSearchText = searchTextData->GetString();
PMString getReplaceText = replaceTextData->GetString();
Utils<IScriptArgs>()->Set("searchText", getSearchText);
Utils<IScriptArgs>()->Set("replaceText", getReplaceText);
PMString scriptCode2;
scriptCode2.Append("var searchText= app.scriptArgs.get(\"searchText\");");
scriptCode2.Append("var replaceText= app.scriptArgs.get(\"replaceText\");");
scriptCode2.Append("app.findGrepPreferences = NothingEnum.NOTHING;");
scriptCode2.Append("app.findGrepPreferences.findWhat = \"\\ < \" + searchText;");
scriptCode2.Append("var list = app.activeDocument.findGrep();");
scriptCode2.Append("app.findGrepPreferences.findWhat = \"\\ < \" + searchText;");
scriptCode2.Append("app.changeGrepPreferences.changeTo = replaceText;");
scriptCode2.Append("app.activeDocument.changeGrep();");
scriptCode2.Append("app.findGrepPreferences = NothingEnum.NOTHING;");
scriptCode2.Append("alert(list.length + \" replacement's made\");");
PMString engineName("myengine");
int32 errorCode = Utils<IExtendScriptUtils>()->RunScriptInEngine(engineName, scriptCode2);
}
if (RadioButton4->IsSelected() == kTrue)
{
CAlert::InformationAlert("Case Sensitive Ends with");
InterfacePtr<IScriptManager> scriptManager(Utils<IScriptUtils>()->QueryScriptManager(kJavaScriptMgrBoss));
InterfacePtr<IScriptRunner> scriptRunner(scriptManager, UseDefaultIID());
PMString getSearchText = searchTextData->GetString();
PMString getReplaceText = replaceTextData->GetString();
Utils<IScriptArgs>()->Set("searchText", getSearchText);
Utils<IScriptArgs>()->Set("replaceText", getReplaceText);
PMString scriptCode3;
scriptCode3.Append("var searchText= app.scriptArgs.get(\"searchText\");");
scriptCode3.Append("var replaceText= app.scriptArgs.get(\"replaceText\");");
scriptCode3.Append("app.findGrepPreferences = NothingEnum.NOTHING;");
scriptCode3.Append("app.findGrepPreferences.findWhat = searchText + \"\\ > \";");
scriptCode3.Append("var list = app.activeDocument.findGrep();");
scriptCode3.Append("app.findGrepPreferences.findWhat = searchText + \"\\ > \";");
scriptCode3.Append("app.changeGrepPreferences.changeTo = replaceText;");
scriptCode3.Append("app.activeDocument.changeGrep();");
scriptCode3.Append("app.findGrepPreferences = NothingEnum.NOTHING;");
scriptCode3.Append("alert(list.length + \" replacement's made\");");
PMString engineName("myengine");
int32 errorCode = Utils<IExtendScriptUtils>()->RunScriptInEngine(engineName, scriptCode3);
}
break(kFalse);
}//Case sensitive
if (CheckBox1->IsSelected() == kFalse)
{
if (RadioButton1->IsSelected() == kTrue)
{
CAlert::InformationAlert("Case InSensitive Contains With");
InterfacePtr<IScriptManager> scriptManager(Utils<IScriptUtils>()->QueryScriptManager(kJavaScriptMgrBoss));
InterfacePtr<IScriptRunner> scriptRunner(scriptManager, UseDefaultIID());
PMString getSearchText = searchTextData->GetString();
PMString getReplaceText = replaceTextData->GetString();
Utils<IScriptArgs>()->Set("searchText", getSearchText);
Utils<IScriptArgs>()->Set("replaceText", getReplaceText);
PMString scriptCode4;
scriptCode4.Append("var searchText= app.scriptArgs.get(\"searchText\");");
scriptCode4.Append("var replaceText= app.scriptArgs.get(\"replaceText\");");
scriptCode4.Append("app.findGrepPreferences = NothingEnum.NOTHING;");
scriptCode4.Append("app.findGrepPreferences.findWhat = \"(?i)\"+searchText;");
scriptCode4.Append("var list = app.activeDocument.findGrep();");
scriptCode4.Append("app.findGrepPreferences.findWhat = \"(?i)\"+searchText;");
scriptCode4.Append("app.changeGrepPreferences.changeTo = replaceText;");
scriptCode4.Append("app.activeDocument.changeGrep();");
scriptCode4.Append("var replaceLength = list.length;");
scriptCode4.Append("app.findGrepPreferences = NothingEnum.NOTHING;");
scriptCode4.Append("alert(list.length + \" replacement's made\");");
PMString engineName("myengine");
int32 errorCode = Utils<IExtendScriptUtils>()->RunScriptInEngine(engineName, scriptCode4);
}
if (RadioButton2->IsSelected() == kTrue)
{
CAlert::InformationAlert("Case InSensitive Exact");
InterfacePtr<IScriptManager> scriptManager(Utils<IScriptUtils>()->QueryScriptManager(kJavaScriptMgrBoss));
InterfacePtr<IScriptRunner> scriptRunner(scriptManager, UseDefaultIID());
PMString getSearchText = searchTextData->GetString();
PMString getReplaceText = replaceTextData->GetString();
Utils<IScriptArgs>()->Set("searchText", getSearchText);
Utils<IScriptArgs>()->Set("replaceText", getReplaceText);
PMString scriptCode5;
scriptCode5.Append("var searchText= app.scriptArgs.get(\"searchText\");");
scriptCode5.Append("var replaceText= app.scriptArgs.get(\"replaceText\");");
scriptCode5.Append("app.findGrepPreferences = NothingEnum.NOTHING;");
scriptCode5.Append("app.findGrepPreferences.findWhat = \"\\b\"+\"(?i)\"+searchText+\"\\b\";");
scriptCode5.Append("var list = app.activeDocument.findGrep();");
scriptCode5.Append("app.findGrepPreferences.findWhat = \"\\b\"+\"(?i)\"+searchText+\"\\b\";");
scriptCode5.Append("app.changeGrepPreferences.changeTo = replaceText;");
scriptCode5.Append("app.activeDocument.changeGrep();");
scriptCode5.Append("var replaceLength = list.length;");
scriptCode5.Append("app.findGrepPreferences = NothingEnum.NOTHING;");
scriptCode5.Append("alert(list.length + \" replacement's made\");");
PMString engineName("myengine");
int32 errorCode = Utils<IExtendScriptUtils>()->RunScriptInEngine(engineName, scriptCode5);
}
if (RadioButton3->IsSelected() == kTrue)
{
CAlert::InformationAlert("Case InSensitive Starts with");
InterfacePtr<IScriptManager> scriptManager(Utils<IScriptUtils>()->QueryScriptManager(kJavaScriptMgrBoss));
InterfacePtr<IScriptRunner> scriptRunner(scriptManager, UseDefaultIID());
PMString getSearchText = searchTextData->GetString();
PMString getReplaceText = replaceTextData->GetString();
Utils<IScriptArgs>()->Set("searchText", getSearchText);
Utils<IScriptArgs>()->Set("replaceText", getReplaceText);
PMString scriptCode6;
scriptCode6.Append("var searchText= app.scriptArgs.get(\"searchText\");");
scriptCode6.Append("var replaceText= app.scriptArgs.get(\"replaceText\");");
scriptCode6.Append("app.findGrepPreferences = NothingEnum.NOTHING;");
scriptCode6.Append("app.findGrepPreferences.findWhat = \"\\<\"+\"(?i)\"+searchText;");
scriptCode6.Append("var list = app.activeDocument.findGrep();");
scriptCode6.Append("app.findGrepPreferences.findWhat = \"\\<\"+\"(?i)\"+searchText;");
scriptCode6.Append("app.changeGrepPreferences.changeTo = replaceText;");
scriptCode6.Append("app.activeDocument.changeGrep();");
scriptCode6.Append("var replaceLength = list.length;");
scriptCode6.Append("app.findGrepPreferences = NothingEnum.NOTHING;");
scriptCode6.Append("alert(list.length + \" replacement's made\");");
PMString engineName("myengine");
int32 errorCode = Utils<IExtendScriptUtils>()->RunScriptInEngine(engineName, scriptCode6);
}
if (RadioButton4->IsSelected() == kTrue)
{
CAlert::InformationAlert("Case Insensitive Ends with");
InterfacePtr<IScriptManager> scriptManager(Utils<IScriptUtils>()->QueryScriptManager(kJavaScriptMgrBoss));
InterfacePtr<IScriptRunner> scriptRunner(scriptManager, UseDefaultIID());
PMString getSearchText = searchTextData->GetString();
PMString getReplaceText = replaceTextData->GetString();
Utils<IScriptArgs>()->Set("searchText", getSearchText);
Utils<IScriptArgs>()->Set("replaceText", getReplaceText);
PMString scriptCode7;
scriptCode7.Append("var searchText= app.scriptArgs.get(\"searchText\");");
scriptCode7.Append("var replaceText= app.scriptArgs.get(\"replaceText\");");
scriptCode7.Append("app.findGrepPreferences = NothingEnum.NOTHING;");
scriptCode7.Append("app.findGrepPreferences.findWhat = \"(?i)\"+searchText+\"\\>\";");
scriptCode7.Append("var list = app.activeDocument.findGrep();");
scriptCode7.Append("app.findGrepPreferences.findWhat = \"(?i)\"+searchText\"\\>\";");
scriptCode7.Append("app.changeGrepPreferences.changeTo = replaceText;");
scriptCode7.Append("app.activeDocument.changeGrep();");
scriptCode7.Append("var replaceLength = list.length;");
scriptCode7.Append("app.findGrepPreferences = NothingEnum.NOTHING;");
scriptCode7.Append("alert(list.length + \" replacement's made\");");
PMString engineName("myengine");
int32 errorCode = Utils<IExtendScriptUtils>()->RunScriptInEngine(engineName, scriptCode7);
}
break(kFalse);
}//Case InSensitive
}//search text data
}//replace text data
}//replace button
}
}
} while (false);
}

 

Thanks,

-Jothi.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Advisor ,
Aug 07, 2020

Copy link to clipboard

Copied

Wearing a scripter's hat, it might be a good idea to reset the find / change prefs to some initial state.

As plug-in developer I'd say to stick with plug-in methods instead of calling scripts, even if it takes a bit effort to figure out the right API for each feature. Don't fight two learning curves at the same time.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Aug 07, 2020

Copy link to clipboard

Copied

Hi Dirk,

resetting prefs is one of a couple of things I would do.

 

What else could go wrong inspite of this effort?

FindChangeGrepOptions are set not to look into footnote texts and all possible texts one could find are actually in footnote texts. Or locked layers are omitted, or, or, or…

Note: We absolutely know nothing about the target InDesign document.

 

Regards,
Uwe Laubender

( ACP )

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...