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

learning scripting with javascript create file

Community Beginner ,
Aug 03, 2024 Aug 03, 2024
I'm learning scripting in photoshop and using the scripting guide from 2020. The new File javascript doesn't create the file, and the script fails. Could it be that this is no longer supported? Is there other way to create a file?
I will try to test on a windows machine o see if there is any difference.

 

var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;


filePath = "/Users/$USERNAME/Desktop/myfile1.pdf"
var fileRef = new File(filePath);


var pdfOpenOptions =  new PDFOpenOptions
pdfOpenOptions.antiAlias = true
pdfOpenOptions.mode = OpenDocumentMode.RGB
pdfOpenOptions.resolution = 72
pdfOpenOptions.page = 3

app.open( fileRef, pdfOpenOptions )
app.preferences.rulerUnits = originalRulerUnits​

 

 

 
TOPICS
Actions and scripting , macOS
558
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

correct answers 2 Correct answers

Community Expert , Aug 03, 2024 Aug 03, 2024

Try changing this:

 

filePath = "/Users/$USERNAME/Desktop/myfile1.pdf"
var fileRef = new File(filePath);

 

To this:

 

filePath = "~/Desktop/myfile1.pdf";
var fileRef = new File(filePath);
alert(fileRef.exists);

 

What is the result of the alert?

 

https://theiviaxx.github.io/photoshop-docs/Core/File.html

 

Translate
Community Expert , Aug 04, 2024 Aug 04, 2024

I think that there's a misunderstanding in your expectations. That's the correct and expected result of the test. False is the result if there is no file at the path of the file object. The new File() is a programming object used to work with a literal file, not a method to create a literal file. To create a literal file, you would need to create a new document and save it using the appropriate file format save options.

Translate
Adobe
Community Expert ,
Aug 03, 2024 Aug 03, 2024

Try changing this:

 

filePath = "/Users/$USERNAME/Desktop/myfile1.pdf"
var fileRef = new File(filePath);

 

To this:

 

filePath = "~/Desktop/myfile1.pdf";
var fileRef = new File(filePath);
alert(fileRef.exists);

 

What is the result of the alert?

 

https://theiviaxx.github.io/photoshop-docs/Core/File.html

 

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 ,
Aug 04, 2024 Aug 04, 2024

I tried to run it and getting False.  I also checked the path of Desktop and the file wasn't created.

 

I tried search extend script on Mac and there seem to be some limitations. I used VS code to run extend scipt with small tool someone developed. I can't run the official extend script debugger because it only suppport Intel chips. My machine it too weak to run Rosetta with VSCODE which would probably solved this problem.. 

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 ,
Aug 04, 2024 Aug 04, 2024

I think that there's a misunderstanding in your expectations. That's the correct and expected result of the test. False is the result if there is no file at the path of the file object. The new File() is a programming object used to work with a literal file, not a method to create a literal file. To create a literal file, you would need to create a new document and save it using the appropriate file format save options.

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 ,
Aug 04, 2024 Aug 04, 2024

OK, thanks, I understand. But this excercise still not working.  It fails on app.open( filleRef, pdfOpenOptions ) yelling it does expect an existing file: 

Error 1233: Expected a reference to an existing File/Folder

 

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 ,
Aug 04, 2024 Aug 04, 2024

If the file has the correct case-sensitive spelling at the path and exists = true, then the code works.

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 ,
Aug 05, 2024 Aug 05, 2024

yeah, I kinda mixed up with open, where it use in code sometimes "creating a file". But here it means open an already created file. Thanks for the help.

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
LEGEND ,
Aug 05, 2024 Aug 05, 2024

Use app.documents.add() to create a new Photoshop document, then you can save or export to a pdf.

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 ,
Aug 05, 2024 Aug 05, 2024

As @maya293983654m0v was attempting to open page 3 of a PDF, if the PDF doesn't exist and needs to be created and saved, it will take more work than a standard image:

 

https://theiviaxx.github.io/photoshop-docs/Photoshop/PresentationOptions/presentation.html

 

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
LEGEND ,
Aug 06, 2024 Aug 06, 2024

A newly created file would be blank and have no page 3 😄

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 ,
Aug 06, 2024 Aug 06, 2024
quote

A newly created file would be blank and have no page 3 😄


By Lumigraphics


That's why I was saying that it would take more work to create a multipage PDF.

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
LEGEND ,
Aug 06, 2024 Aug 06, 2024

Can Photoshop even create a multipage PDF?

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 ,
Aug 06, 2024 Aug 06, 2024
LATEST
quote

Can Photoshop even create a multipage PDF?


By Lumigraphics

 

Yes, it can, that's why I linked to the PDF PresentationOptions() DOM code reference earlier in the topic.

 

Here's a topic where I merge file pairs to create multiple 2-page front/back PDF files. 

 

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