• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Please Help Fixing the Code

Enthusiast ,
Jun 14, 2020 Jun 14, 2020

Copy link to clipboard

Copied

Dear Professionals

Please i need the help for fixing that code, the code is letting user select any text have paragraph style then export all the pages have the same style as jpg, it works fine when i write the path directly but when i let the user to select the path it only export one file!, here is the code :

 

if (app.documents.length != 0){
var myDoc = app.activeDocument;
MakeJPEGfile();
} else {
alert("Please open a document and try again.");
}
function myPS() {
try {
return myDoc.selection[0].appliedParagraphStyle;
} catch (e) {
alert("Place cursor to text with paragraph style for filenames");
exit();
}
}
function MakeJPEGfile() {
app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.high;
app.jpegExportPreferences.exportResolution = 300;
app.jpegExportPreferences.jpegExportRange = ExportRangeOrAllPages.exportRange;
app.findGrepPreferences = null;
app.findGrepPreferences.appliedParagraphStyle = myPS();
var f = myDoc.findGrep();
for (var myCounter = 0; myCounter < f.length; myCounter++) {
try {
var curPage = f[myCounter].parentTextFrames[0].parentPage;
if (curPage.appliedSection.name != "") {
curPage.appliedSection.name = "";
}
var objName = f[myCounter].contents.replace(/ /g,"_").toLowerCase();
app.jpegExportPreferences.pageString = curPage.name;
//------------------------------------------------------------------------------
//Select a Folder
//New from M.Hasanin-Select Folder to Save
var myPrompt = prompt("Name your folder to create on the desktop");
var f = new Folder("~/Desktop/" + myPrompt);
if (!f.exists)
f.create();
//End Select Folder
var myFilePath = f + "/" + objName + ".jpg"; //export to a folder of the current document
var myFile = new File(myFilePath);
//------------------------------------------------------------------------------
//var myFilePath = "~/Desktop/" + "/" + objName + ".jpg"; //export to a folder of the current document
//var myFile = new File(myFilePath);
myDoc.exportFile(ExportFormat.jpg, myFile, false);
}
catch(e){}//pasteboard?
}
}

 

Please help to find where is the Wrong! 

Best
Mohammad Hasanin
TOPICS
Scripting

Views

299

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
community guidelines

correct answers 1 Correct answer

Enthusiast , Jun 17, 2020 Jun 17, 2020

You have to change variable name:
var f = myDoc.findGrep();

vs

var f = new Folder("~/Desktop/" + myPrompt);

Votes

Translate

Translate
Enthusiast ,
Jun 14, 2020 Jun 14, 2020

Copy link to clipboard

Copied

Dear Pros..

Please Help me in Fixing the Code, i Can Export the *.jpg's to Desktop Directly But When Make User Select a folder it Only Export One JPG, I dont Know what is the Wrong? I Spend Hours Trying to Fix the Problem , Here is the Code :

if (app.documents.length != 0){
var myDoc = app.activeDocument;
MakeJPEGfile();
} else {
alert("Please open a document and try again.");
}
function myPS() {
try {
return myDoc.selection[0].appliedParagraphStyle;
} catch (e) {
alert("Place cursor to text with paragraph style for filenames");
exit();
}
}
function MakeJPEGfile() {
app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.high;
app.jpegExportPreferences.exportResolution = 300;
app.jpegExportPreferences.jpegExportRange = ExportRangeOrAllPages.exportRange;
app.findGrepPreferences = null;
app.findGrepPreferences.appliedParagraphStyle = myPS();
var f = myDoc.findGrep();
for (var myCounter = 0; myCounter < f.length; myCounter++) {
try {
var curPage = f[myCounter].parentTextFrames[0].parentPage;
if (curPage.appliedSection.name != "") {
curPage.appliedSection.name = "";
}
var objName = f[myCounter].contents.replace(/ /g,"_").toLowerCase();
app.jpegExportPreferences.pageString = curPage.name;
//------------------------------------------------------------------------------
//Select a Folder
//New from M.Hasanin-Select Folder to Save
var myPrompt = prompt("Name your folder to create on the desktop");
var f = new Folder("~/Desktop/" + myPrompt);
if (!f.exists)
f.create();
//End Select Folder
//------------------------------------------------------------------------------
//for (var objName = 0; objName < myPS() ; objName++) {
 var myFilePath = f.fsName + "/" + objName + ".jpg"; //export to a folder of the current document
  //var myFilePath = f.fsName + objName + ".jpg"; //export to a folder of the current document
  var myFile = new File(myFilePath);
//}
//------------------------------------------------------------------------------
//var myFilePath = "~/Desktop/" + "/" + objName + ".jpg"; //export to a folder of the current document
//var myFile = new File(myFilePath);
myDoc.exportFile(ExportFormat.jpg, myFile, false);
}
catch(e){}//pasteboard?
}
}

Any Help is Appreciated

Best Regards

 

Best
Mohammad Hasanin

Votes

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
community guidelines
Community Expert ,
Jun 15, 2020 Jun 15, 2020

Copy link to clipboard

Copied

I'd $.writeln on your last catch statement to find out what the error is. Seems like an error would be happening. One potential cause is that you have two variables named "f" within the same scope: your grep finds and your folder. 

Votes

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
community guidelines
Enthusiast ,
Jun 17, 2020 Jun 17, 2020

Copy link to clipboard

Copied

LATEST

You have to change variable name:
var f = myDoc.findGrep();

vs

var f = new Folder("~/Desktop/" + myPrompt);

Votes

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
community guidelines