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

Check the file type!

Engaged ,
May 23, 2019 May 23, 2019

Hello everyone! Once I open a document, is there any way to check the file type such as: jpg, tiff, png ...?

Thank you!

TOPICS
Actions and scripting
2.5K
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

Guide , May 23, 2019 May 23, 2019

#target photoshop;

if(documents.length) alert(fileType());

function fileType(){

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var desc = executeActionGet(ref);

try{

return desc.getString(stringIDToTypeID("format"));

}catch(e){return "Unsaved File";}

};


Translate
Community Expert , Aug 25, 2025 Aug 25, 2025

This was a very strange thread. Why not just look at the title bar? Or turn on visible extensions in the operating system, so you know before even opening the file?

Translate
Adobe
LEGEND ,
May 23, 2019 May 23, 2019

activeDocument.name.split(/\./)[1]

or for many dots:

activeDocument.name.match(/[^.]+$/)[0]

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
Engaged ,
May 23, 2019 May 23, 2019

It was not quite that! Just an alert type message stating something like this: "This document is a jpg or tiff or png extension file ..." just show me its original format

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 ,
May 23, 2019 May 23, 2019

Then after opening command why don't you put one of proposed line surrounded by appropraite text to alert() ?

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
Engaged ,
May 23, 2019 May 23, 2019

Could you show me an example? Please

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 ,
May 23, 2019 May 23, 2019

alert('File extension: ' + open(File(Folder.desktop + '/someFile.jpg')).name.match(/[^.]+$/)[0])

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
Guide ,
May 23, 2019 May 23, 2019

#target photoshop;

if(documents.length) alert(fileType());

function fileType(){

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var desc = executeActionGet(ref);

try{

return desc.getString(stringIDToTypeID("format"));

}catch(e){return "Unsaved File";}

};


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
Engaged ,
May 23, 2019 May 23, 2019

Great job SuperMerlin! That's what I'm looking for. Thank you.

Kukurykus thanks for listening.

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 ,
May 23, 2019 May 23, 2019

Doesn't my code do the same, that you wanted?

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
Engaged ,
May 24, 2019 May 24, 2019

Hi Kukurykus , sorry for the late reply, the SuperMerlin script works perfectly regardless of where the document originated. yet I am grateful for your help. Thank you.

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 ,
May 24, 2019 May 24, 2019

I ask, because when you use the code in the alert from my very first reply you're getting the same:

alert(activeDocument.name.match(/[^.]+$/)[0])

so regardless of its origin, or what else do you mean by that?

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
Engaged ,
May 24, 2019 May 24, 2019

You really are right!

alert (activeDocument.name.match (/ [^.] + $ /) [0])

I confess I did not understand, so I get a lot easier.

Both of these situations worked, to whom do I owe the correct answer credits?

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 ,
May 24, 2019 May 24, 2019

I wanted to understand what you really needed, because I was sure you know how alert can be used, so in the first post I simply didn't use that what seemed to be obvious. You decide who deserves credits

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
Explorer ,
Aug 25, 2025 Aug 25, 2025

Old thread, but replying as this was the top result on google for me, so may prove useful to others. SuperMerlin's script works perfectly but is extremely slow on a larger PSB (4-5s for me). Pulling just the format instead of the whole document descriptor sped it up quite a bit:

function getFileFormatFast () {
    var s2t = stringIDToTypeID, c2t = charIDToTypeID;
    try {
        var ref = new ActionReference();
        // Ask ONLY for the 'format' property of the target document
        ref.putProperty(s2t('property'), s2t('format'));
        ref.putEnumerated(c2t('Dcmn'), s2t('ordinal'), s2t('targetEnum'));

        var desc = executeActionGet(ref);
        return desc.hasKey(s2t('format')) ? desc.getString(s2t('format')) : 'Unsaved File';
    } catch (e) {
        return 'Unsaved File';
    }
}
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 25, 2025 Aug 25, 2025

This was a very strange thread. Why not just look at the title bar? Or turn on visible extensions in the operating system, so you know before even opening the file?

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
Explorer ,
Aug 25, 2025 Aug 25, 2025

Because if I'm scripting something on dozens of files and want it to handle things separately depending on if it's a layered PSD already or not, I don't want to have to tell it every time!

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 25, 2025 Aug 25, 2025

OK, got 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
Valorous Hero ,
Aug 25, 2025 Aug 25, 2025
LATEST

 I always tell people to do it however they want, as long as they get an answer, it works for them, or the most important:  You like it.  


2025-08-25_163734.png

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