Skip to main content
mariap81355074
Inspiring
November 20, 2018
Answered

if ($.os.search(/windows/i) change to Mac?

  • November 20, 2018
  • 3 replies
  • 768 views

Hi guys! I have a script I used a lot on my pc, but now I'm on a Mac it's only letting me select .ai files instead of .png or .psd or .jpeg files that I need. I think it's down to this line

  if (theLayer.kind != "LayerKind.SMARTOBJECT") {alert ("selected layer is not a smart object")} 

    else { 

    if ($.os.search(/windows/i) != -1) {var theFiles = File.openDialog ("please select files", "*.pdf;*.ai;*.psd;*.tif;*.jpg;*.png", true)} 

    else {var theFiles = File.openDialog ("please select files", getFiles, true)}; 

    if (theFiles) { 

I've tried changing with the JS reference but nothing seems to work for me. I can post the whole script if necessary.

Thanks so much guys

This topic has been closed for replies.
Correct answer JJMack

I really appreciate your time and I'm sorry to be so confusing. I was wrong in assuming the windows is causing the error. I am using a different Mac today so I had forgotten my settings had changed.

However, in the ".png" area, changing the extension type case still doesn't cause anything to change. It still won't accept .png files. It will accept .psd and .jpegs though. I have checked my other scrips and the guide for proper extensions and it seems .png should be fine so I'm not sure why it isn't working.


Yes getFile maye be it seems like it may be a function that may be filtering the file selected look at the bottom of your script

////// get psds, tifs and jpgs from files //////

function getFiles (theFile) {

    if (theFile.name.match(/\.(pdf|ai|jpg|tif|psb|psd)$/i)) {

       return true

    };

};

3 replies

Legend
November 20, 2018

Try this code for the getFiles filter function.

function getFiles (theFile)

    {

    if (theFile.name.match(/\.(pdf|ai|jpg|tif|psb|psd|png)$/i)) return true;

    if (theFile instanceof Folder) return true;

  

    return false;

    }

I can not check because of the inaccessibility of the Mac

JJMack
Community Expert
Community Expert
November 20, 2018

Then what do you think  this statement does

    if ($.os.search(/windows/i) != -1) {var theFiles = File.openDialog ("please select files", "*.pdf;*.ai;*.psd;*.tif;*.jpg;*.png", true)}

JJMack
mariap81355074
Inspiring
November 20, 2018

I haven't used the script in a few years, and .ai wasn't included initially, and it still only let me choose .ai files. I thought it was part of an alert box since it was in quotation marks. Does the 'os' and 'windows' not need to change to work on a Mac?

.jpg should at least work since I have it in other scripts I use on the Mac. I can't understand what I'm doing wrong

JJMack
Community Expert
Community Expert
November 20, 2018

That statement is for windows the mac part is

else {var theFiles = File.openDialog ("please select files", getFiles, true)};

I do not know if that is correct or if the test for windows is correct on  your mac

if ($.os.search(/windows/i) != -1)

I know ther are differences between Mac and Windows.  I do code my script to do an anyting different on a Mac.  My File open Dialog with the etension filter seemd to work on my son's mac.  However I have seen documentation that state  Selection filters are not supported on Mac.

The code in this script seen to be coded not to use a filter on a Mac.

The thing is I do not know if the code is correct.

JJMack
JJMack
Community Expert
Community Expert
November 20, 2018

That not a full script.   Make sure Mac use the same file extensions png files.  I know for my son I had to add .jpe for jpeg files on his Mac. Mac unix file system may also be case sensitive you may need to add PNG png pNg  PNg  pnG pNG.  Mac file open dialog may not support a extension filter.   There are some differences between Windows and Mac files system support.

JJMack does not use a Mac the last Mac I used was back when Apple was using their own OS designs OS7 and OS9.  I chose Windows NT  then Windows 2000.

JJMack
mariap81355074
Inspiring
November 20, 2018

Thanks for your time. There's no specification of what file you can choose I don't think, so that's why I thought the error was with the 'windows' line.

Here's the full script

    // select files and place in a selected smart object; 

    // to cause the so in the so to be scaled to fit the resolution is temprarily changed, but this is no guarantee for this to happen; 

    // 2015, use it at your own risk; 

    #target photoshop 

    if (app.documents.length > 0) { 

    var myDocument = app.activeDocument; 

    var theName= myDocument.name.match(/(.*)\.[^\.]+$/)[1]; 

    var thePath = myDocument.path; 

    var theLayer = myDocument.activeLayer; 

    // psd options; 

    psdOpts = new PhotoshopSaveOptions(); 

    psdOpts.embedColorProfile = true; 

    psdOpts.alphaChannels = true; 

    psdOpts.layers = true; 

    psdOpts.spotColors = true; 

    // check if layer is smart object; 

    if (theLayer.kind != "LayerKind.SMARTOBJECT") {alert ("selected layer is not a smart object")} 

    else { 

    if ($.os.search(/windows/i) != -1) {var theFiles = File.openDialog ("please select files", "*.pdf;*.ai;*.psd;*.tif;*.jpg;*.png", true)} 

    else {var theFiles = File.openDialog ("please select files", getFiles, true)}; 

    if (theFiles) { 

    // set to pixels; 

    var originalRulerUnits = app.preferences.rulerUnits; 

    app.preferences.rulerUnits = Units.PIXELS; 

    // get bounds; 

    var theBounds = theLayer.bounds; 

    // work through the array; 

    for (var m = 0; m < theFiles.length; m++) { 

    var theNewName = File(theFiles).name.match(/(.*)\.[^\.]+$/)[1]; 

    // replace smart object; 

    placeSmartObjectInSmartObject(app.activeDocument.activeLayer, theFiles); 

    #target photoshop 

    function main(){ 

        if(!documents.length) return; 

        var doc = activeDocument; 

        var oldPath = activeDocument.path; 

        for(var a=0;a<doc.layerSets.length;a++){ 

            activeDocument.activeLayer = activeDocument.layers.getByName(doc.layerSets.name); 

            dupLayers(); 

            activeDocument.mergeVisibleLayers(); 

            activeDocument.trim(TrimType.TRANSPARENT,true,true,true,true);

  var saveFile= File(oldPath+"/"+doc.layerSets.name.replace("*****", theNewName) +".jpg");

    var fileName = theNewName.slice(0,5);

    if (doc.layerSets.name.indexOf("*****") != -1) {

    var saveFile= File(oldPath+"/"+doc.layerSets.name.replace("*****", fileName) +".jpg");

    }

    else {

    var saveFile= File(oldPath+"/"+doc.layerSets.name+"_"+fileName +".jpg");

    };

  saveJPG(saveFile,10); 

            app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); 

        } 

    } 

    main(); 

     

    function dupLayers() { 

        var desc143 = new ActionDescriptor(); 

        var ref73 = new ActionReference(); 

        ref73.putClass( charIDToTypeID('Dcmn') ); 

        desc143.putReference( charIDToTypeID('null'), ref73 ); 

        desc143.putString( charIDToTypeID('Nm  '), activeDocument.activeLayer.name ); 

        var ref74 = new ActionReference(); 

        ref74.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') ); 

        desc143.putReference( charIDToTypeID('Usng'), ref74 ); 

        executeAction( charIDToTypeID('Mk  '), desc143, DialogModes.NO ); 

    } 

     

     

    function saveJPG(saveFile, jpegQuality) { 

        app.activeDocument.flatten();  

        jpgSaveOptions = new JPEGSaveOptions(); 

        jpgSaveOptions.embedColorProfile = true; 

        jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; 

        jpgSaveOptions.matte = MatteType.NONE; 

        jpgSaveOptions.quality = jpegQuality; //1-12 

        activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE); 

    } 

// undo; 

myDocument.activeHistoryState = myDocument.historyStates[myDocument.historyStates.length-2] 

}; 

// reset; 

originalRulerUnits = app.preferences.rulerUnits; 

}; 

    ////// open smart object ////// 

    function placeSmartObjectInSmartObject (theLayer, thisFile) { 

    if (theLayer.kind == "LayerKind.SMARTOBJECT") { 

    // get prefernces setting; 

    var ref = new ActionReference(); 

    ref.putProperty(stringIDToTypeID("property"), stringIDToTypeID("generalPreferences")); 

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

    var applicationDesc = executeActionGet(ref); 

    var generalDesc = applicationDesc.getObjectValue(stringIDToTypeID("generalPreferences")); 

    var resizeDuringPlace = generalDesc.getBoolean(stringIDToTypeID("resizePastePlace")); 

    if (resizeDuringPlace == false) { 

    // ======================================================= 

    var idsetd = charIDToTypeID( "setd" ); 

        var desc1 = new ActionDescriptor(); 

        var idnull = charIDToTypeID( "null" ); 

            var ref1 = new ActionReference(); 

            var idPrpr = charIDToTypeID( "Prpr" ); 

            var idGnrP = charIDToTypeID( "GnrP" ); 

            ref1.putProperty( idPrpr, idGnrP ); 

            var idcapp = charIDToTypeID( "capp" ); 

            var idOrdn = charIDToTypeID( "Ordn" ); 

            var idTrgt = charIDToTypeID( "Trgt" ); 

            ref1.putEnumerated( idcapp, idOrdn, idTrgt ); 

        desc1.putReference( idnull, ref1 ); 

        var idT = charIDToTypeID( "T  " ); 

            var desc2 = new ActionDescriptor(); 

            var idresizePastePlace = stringIDToTypeID( "resizePastePlace" ); 

            desc2.putBoolean( idresizePastePlace, true ); 

            var idlegacyPathDrag = stringIDToTypeID( "legacyPathDrag" ); 

            desc2.putBoolean( idlegacyPathDrag, true ); 

        var idGnrP = charIDToTypeID( "GnrP" ); 

        desc1.putObject( idT, idGnrP, desc2 ); 

    executeAction( idsetd, desc1, DialogModes.NO ); 

    }; 

    // ======================================================= 

    var idplacedLayerEditContents = stringIDToTypeID( "placedLayerEditContents" ); 

        var desc2 = new ActionDescriptor(); 

    executeAction( idplacedLayerEditContents, desc2, DialogModes.NO ); 

    // set to high resolution to make scaling on placing probable; 

    var theResolution = activeDocument.resolution; 

    activeDocument.resizeImage(undefined, undefined, 3000, ResampleMethod.NONE); 

    // place so; 

    // ======================================================= 

    var idPlc = charIDToTypeID( "Plc " ); 

        var desc4 = new ActionDescriptor(); 

        var idAs = charIDToTypeID( "As  " ); 

            var desc5 = new ActionDescriptor(); 

            var idfsel = charIDToTypeID( "fsel" ); 

            var idpdfSelection = stringIDToTypeID( "pdfSelection" ); 

            var idpage = stringIDToTypeID( "page" ); 

            desc5.putEnumerated( idfsel, idpdfSelection, idpage ); 

            var idPgNm = charIDToTypeID( "PgNm" ); 

            desc5.putInteger( idPgNm, 1 ); 

            var idCrop = charIDToTypeID( "Crop" ); 

            var idcropTo = stringIDToTypeID( "cropTo" ); 

            var idtrimBox = stringIDToTypeID( "trimBox" ); 

            desc5.putEnumerated( idCrop, idcropTo, idtrimBox ); 

        var idPDFG = charIDToTypeID( "PDFG" ); 

        desc4.putObject( idAs, idPDFG, desc5 ); 

        var idnull = charIDToTypeID( "null" ); 

        desc4.putPath( idnull, new File( thisFile ) ); 

        var idFTcs = charIDToTypeID( "FTcs" ); 

        var idQCSt = charIDToTypeID( "QCSt" ); 

        var idQcsa = charIDToTypeID( "Qcsa" ); 

        desc4.putEnumerated( idFTcs, idQCSt, idQcsa ); 

        var idOfst = charIDToTypeID( "Ofst" ); 

            var desc6 = new ActionDescriptor(); 

            var idHrzn = charIDToTypeID( "Hrzn" ); 

            var idRlt = charIDToTypeID( "#Rlt" ); 

            desc6.putUnitDouble( idHrzn, idRlt, 0.000000 ); 

            var idVrtc = charIDToTypeID( "Vrtc" ); 

            var idRlt = charIDToTypeID( "#Rlt" ); 

            desc6.putUnitDouble( idVrtc, idRlt, 0.000000 ); 

        var idOfst = charIDToTypeID( "Ofst" ); 

        desc4.putObject( idOfst, idOfst, desc6 ); 

        var idAntA = charIDToTypeID( "AntA" ); 

        desc4.putBoolean( idAntA, true ); 

    executeAction( idPlc, desc4, DialogModes.NO ); 

    // toggle visibility of others; 

    // ======================================================= 

    var idShw = charIDToTypeID( "Shw " ); 

        var desc10 = new ActionDescriptor(); 

        var idnull = charIDToTypeID( "null" ); 

            var list4 = new ActionList(); 

                var ref7 = new ActionReference(); 

                var idLyr = charIDToTypeID( "Lyr " ); 

                var idOrdn = charIDToTypeID( "Ordn" ); 

                var idTrgt = charIDToTypeID( "Trgt" ); 

                ref7.putEnumerated( idLyr, idOrdn, idTrgt ); 

            list4.putReference( ref7 ); 

        desc10.putList( idnull, list4 ); 

        var idTglO = charIDToTypeID( "TglO" ); 

        desc10.putBoolean( idTglO, true ); 

    executeAction( idShw, desc10, DialogModes.NO ); 

    // reset resolution; 

    activeDocument.resizeImage(undefined, undefined, theResolution, ResampleMethod.NONE); 

    // close; 

    app.activeDocument.close(SaveOptions.SAVECHANGES) 

    }; 

    }; 

    ////// get psds, tifs and jpgs from files ////// 

    function getFiles (theFile) { 

    if (theFile.name.match(/\.(pdf|ai|jpg|tif|psb|psd)$/i)) { 

    return true 

    }; 

    };