Copy link to clipboard
Copied
Hello everyone! Once I open a document, is there any way to check the file type such as: jpg, tiff, png ...?
Thank you!
#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";}
};
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?
Copy link to clipboard
Copied
activeDocument.name.split(/\./)[1]
or for many dots:
activeDocument.name.match(/[^.]+$/)[0]
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Then after opening command why don't you put one of proposed line surrounded by appropraite text to alert() ?
Copy link to clipboard
Copied
Could you show me an example? Please
Copy link to clipboard
Copied
alert('File extension: ' + open(File(Folder.desktop + '/someFile.jpg')).name.match(/[^.]+$/)[0])
Copy link to clipboard
Copied
#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";}
};
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Doesn't my code do the same, that you wanted?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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';
}
}
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
OK, got it 🙂
Copy link to clipboard
Copied
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.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now