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

AppleScript/JavaScript to Export PNG, JPG, PDF at Custom DPI – Error at 300 DPI

Community Beginner ,
Mar 11, 2025 Mar 11, 2025

Hello Everyone, 

I’m writing a script (AppleScript or JavaScript) to export an active document to PNG, JPG, and PDF in Adobe Illustrator.

 

So far, exporting at 72 DPI works fine, but when I try to set a different DPI (e.g., 300 DPI), I encounter errors.

 

Example Issue (AppleScript, PNG Export)

 

When exporting PNG (PNG24) at 300 DPI, I use:
export current document to file myFile as PNG24 with options {class:PNG24 export options, transparency:true, resolution:300}

However, this results in an error:

“PNG Export Failed: Adobe Illustrator got an error: Can’t get document 1.”

 

What I Need Help With

1. How can I correctly set PNG resolution to 300 DPI in AppleScript?

2. Is there a JavaScript solution that ensures PNG exports at the correct DPI?

3. How can I properly export JPG at a different DPI (e.g., 300 DPI) without Illustrator resizing the image?

 

Would appreciate any insights on how to properly handle custom DPI exports in AppleScript or JavaScript.

 

Thanks in advance!

TOPICS
How-to , Scripting
209
Translate
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
Adobe
Community Expert ,
Mar 11, 2025 Mar 11, 2025

can you share your script to help debugging it?

Translate
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 Beginner ,
Mar 11, 2025 Mar 11, 2025

tell application "Adobe Illustrator"

 

-- Ensure a document is open

if (count of documents) = 0 then

display dialog "No document is open. Please open an AI file and try again." buttons {"OK"} default button "OK"

return

end if

 

-- Reference the active document

set currentDoc to document 1

set docName to name of currentDoc

set docPath to file path of currentDoc

 

-- Extract document folder

set mLen to length of docName

set mLen to -mLen - 1

set docFolder to text 1 thru mLen of (docPath as string)

 

-- Set file names

set pdfFileName to text 1 thru -4 of docName & ".pdf"

set pngFileName to text 1 thru -4 of docName & ".png"

set jpgFileName to text 1 thru -4 of docName & ".jpg"

 

-- Save the document before exporting

try

save currentDoc

on error errorMessage

display dialog "Document Save Failed: " & errorMessage

return

end try

 

-- Export as PNG (72 DPI)

try

export currentDoc to file (docFolder & pngFileName) as PNG24 with options {class:PNG24 export options, transparency:true, artboard clipping:true}

on error errorMessage

display dialog "PNG Export Failed: " & errorMessage

end try

 

-- Export as JPG (72 DPI, Correct Size)

try

set jpgOptions to {class:JPEG export options, quality:100, antialiasing:true, artboard clipping:true} -- Removed horizontal & vertical scale

export currentDoc to file (docFolder & jpgFileName) as JPEG with options jpgOptions

on error errorMessage

display dialog "JPG Export Failed: " & errorMessage

end try

 

-- Export as PDF

try

save currentDoc in file (docFolder & pdfFileName) as pdf with options {class:PDF save options, PDF preset:"High Qy Print_wHyperlinks_and_Inter", preserve editability:false}

on error errorMessage

display dialog "PDF Export Failed: " & errorMessage

end try

 

-- Close the document without saving

try

close currentDoc saving no

on error errorMessage

display dialog "Error Closing Document: " & errorMessage

end try

 

end tell

 

display dialog "Export completed: PDF, JPG (72 DPI, Correct Size), and PNG (72 DPI)"

Translate
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 Beginner ,
Mar 11, 2025 Mar 11, 2025

This one is working.

But with: 

export current document to file myFile as PNG24 with options {class:PNG24 export options, transparency:true, resolution:300}

does not.



 

Translate
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 ,
Mar 11, 2025 Mar 11, 2025

PNG and JPEG options by `export` cannot specify resolution.

If you want to do so, use `exportforscreens` for AppleScript or `exportForScreens` for ExtendScript.

Translate
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 Beginner ,
Mar 12, 2025 Mar 12, 2025

Do you have any sample?

Translate
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 ,
Mar 12, 2025 Mar 12, 2025

After checking again, not sure how to specify the exportforscreens option in AppleScript.

 

If you are basing it on AppleScript, use do javascript to call ExtendScript. For example, an ExtendScript that exports PNG24 would look like this.

/**
  * @File Export for screens sample
  * @Version 1.0.0
  * @author sttk3.com
*/

(function() {
  try {
    if(app.documents.length <= 0) {return ;}
    var doc = app.documents[0] ;

    // 0. Destination folder
    var destinationFolder = new Folder('~/Desktop/sample') ;

    // 1. Options for the file type specified
    var exportForScreensType = ExportForScreensType.SE_PNG24 ;

    // 2. Format settings for PNG24
    var exportForScreensOptionsPNG24 = new ExportForScreensOptionsPNG24() ;
    exportForScreensOptionsPNG24.transparency = true ;
    exportForScreensOptionsPNG24.interlaced = false ;
    exportForScreensOptionsPNG24.antiAliasing = AntiAliasingMethod.ARTOPTIMIZED ;
    exportForScreensOptionsPNG24.scaleType = ExportForScreensScaleType.SCALEBYRESOLUTION ;
    var resolution = 300 ; // ppi
    exportForScreensOptionsPNG24.scaleTypeValue = resolution ;

    // 3. What to export
    var itemToExport = new ExportForScreensItemToExport() ;
    itemToExport.artboards = '1' ;
    itemToExport.document = false ;

    // 4. Prefix
    var prefix = 'prefix_' ;

    doc.exportForScreens(destinationFolder, exportForScreensType, exportForScreensOptionsPNG24, itemToExport, prefix) ;
  } catch(e) {
    alert(e) ;
  }
})() ;
Translate
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 Beginner ,
Mar 13, 2025 Mar 13, 2025
LATEST

Super. Will try modify it for my needs.

Translate
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