Highlighted

ResizeImage in VBA not Working Correctly After Latest Update

New Here ,
Aug 12, 2020

Copy link to clipboard

Copied

Hi there,

 

I have a VBA script that I've been uses for ages. It does a couple of things including resizing the open image in pixels with the value entered by the user.

 

Ever since I updated to the latest 2020 version, the ResizeImage Method in VBA takes the value entered by the user and resizes the image in centremetres, not pixels. I have never had to set the Preferences.TypeUnit to pixels in the script but I did to ensure that wasn't the issue making it resize in centremetres. Still to no avail.

 

So at this stage I'm stuck as it looks like a bug. I cannot get it to resize in pixels at all.

 

Any help would be appreciated.

TOPICS
Actions and scripting

Views

97

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

ResizeImage in VBA not Working Correctly After Latest Update

New Here ,
Aug 12, 2020

Copy link to clipboard

Copied

Hi there,

 

I have a VBA script that I've been uses for ages. It does a couple of things including resizing the open image in pixels with the value entered by the user.

 

Ever since I updated to the latest 2020 version, the ResizeImage Method in VBA takes the value entered by the user and resizes the image in centremetres, not pixels. I have never had to set the Preferences.TypeUnit to pixels in the script but I did to ensure that wasn't the issue making it resize in centremetres. Still to no avail.

 

So at this stage I'm stuck as it looks like a bug. I cannot get it to resize in pixels at all.

 

Any help would be appreciated.

TOPICS
Actions and scripting

Views

98

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Aug 12, 2020 0
Most Valuable Participant ,
Aug 12, 2020

Copy link to clipboard

Copied

Does you script set ruler units to pixels or relies on the users current preference.

JJMack

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 12, 2020 0
New Here ,
Aug 12, 2020

Copy link to clipboard

Copied

Thanks JJMack for the reply.

 

The script does not set the ruler units but relies on the current user preference.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 12, 2020 0
New Here ,
Aug 13, 2020

Copy link to clipboard

Copied

Hi JJmack, what was the reason you asked the question?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 13, 2020 0
New Here ,
Aug 13, 2020

Copy link to clipboard

Copied

Bump*

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 13, 2020 0
Most Valuable Participant ,
Aug 14, 2020

Copy link to clipboard

Copied

Do not depent on the user ruler setting. Save their setting,  set ruler units to pixels, do your resize. Restore the  user ruler units.

JJMack

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 14, 2020 0
New Here ,
Aug 14, 2020

Copy link to clipboard

Copied

JJMack, you are a legend!

 

Setting the RulerUnits to 1 (pixels) fixed the issue.

 

Just a note for others, changing the TypeUnits to pixels (1) didn't help.

 

I end up with this:

appRef.Preferences.RulerUnits = 1

 

Where of course appRef is:

Set appRef = CreateObject("Photoshop.Application")

 

Thanks JJMack! 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 14, 2020 0
Most Valuable Participant ,
Aug 14, 2020

Copy link to clipboard

Copied

When I first started to hack at scripting  I create a hackers script template the main art looks like  this it may give you some ideas who knows the macs code may me prefes wite some casiv code like this:

 
#target photoshop // this command only works in Photoshop CS2 and higher

app.bringToFront();

if (!documents.length) alert('There are no documents open.', 'No Document');
else {  // at least one document exists proceed
// declare Global variables

main();

or
app.activeDocument.suspendHistory('Some History ID like script name','main()');
}

 

///////////////////////////////////////////////////////////////////////////////
//                            main function                                  //
///////////////////////////////////////////////////////////////////////////////
function main() {
	// declare local variables
	var orig_ruler_units = app.preferences.rulerUnits;
	var orig_type_units = app.preferences.typeUnits;
	var orig_display_dialogs = app.displayDialogs;
	app.preferences.rulerUnits = Units.PIXELS;	// Set the ruler units to PIXELS
	app.preferences.typeUnits = TypeUnits.POINTS;   // Set Type units to POINTS
	app.displayDialogs = DialogModes.NO;		// Set Dialogs off
	try { code(); }
	// display error message if something goes wrong
	catch(e) { alert(e + ': on line ' + e.line, 'Script Error', true); }
        // catch(e){$.writeln(e)}  
	app.displayDialogs = orig_display_dialogs;	// Reset display dialogs 
	app.preferences.typeUnits  = orig_type_units;	// Reset ruler units to original settings 
	app.preferences.rulerUnits = orig_ruler_units;	// Reset units to original settings
}
///////////////////////////////////////////////////////////////////////////////
//                           main function end                               //
///////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////
// The real code is embedded into this function so that at any point it can return //
// to the main line function to let it restore users edit environment and end      //
/////////////////////////////////////////////////////////////////////////////////////
function code() {
	if (
}

//////////////////////////////////////////////////////////////////////////////////
//			Helper Functions					//
//////////////////////////////////////////////////////////////////////////////////

 

Also if you are going to be dealing with text and font sizes and figer out space useage etc you may want to save resolition and set resolution to 72  text seems to be base on points thus resolution 72  seem to help me with how I could deal with text.  I seldom deal with text. Text is to comnplex for my little brain. and dom'r forget to restort the document resolution after dealind with text.

 

  

JJMack

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 14, 2020 0
New Here ,
Aug 14, 2020

Copy link to clipboard

Copied

Thanks JJMack. That's a really great idea - starting with a template like that! I should do that from now on. After a lot of learning I've crreated several scripts to date that save a lot of time. The one below resizes an image from the users input then does a soft light sharpen on it (several times). Maybe some will find it useful. Just wonder if VBA is the right path now given Javascript can be used for Microsoft Office scripting as well as Adobe products...

 

Option Explicit

Call main

Sub main()

    Dim appRef, docRef, i, x, layerSetRef, layerRef, resizeInput, determineResize

    Set appRef = CreateObject("Photoshop.Application")
    Set docRef = appRef.ActiveDocument


    ' //////////////////// Set Photoshop preferences
        
    ' Display only error dialogs
    appRef.DisplayDialogs = 3

    ' Beep when finished
    appRef.Preferences.BeepWhenDone = True

    ' Make Photoshop the foreground application
    appRef.Visible = True

    ' Turn off tool tips
    appRef.Preferences.ShowToolTips = False

    ' Set unit of measurement to pixels
    appRef.Preferences.TypeUnits = 1
    appRef.Preferences.RulerUnits = 1

    ' ////////////////////


    ' Get image resize value
    resizeInput = InputBox("Enter image resize value in pixels.", "Resize Value", 1600)

    ' Resize image to input width
    docRef.ResizeImage CInt(resizeInput)

    ' Duplicate background layer four times; set blend mode; _
    ' apply high pass; add masks; invert
    docRef.ArtLayers(1).Duplicate docRef.ActiveLayer, 3
    Set layerRef = docRef.ArtLayers(1)
    docRef.ActiveLayer = layerRef
    layerRef.Name = CStr(1)
    layerRef.ApplyHighPass(0.3)
    layerRef.BlendMode = 13
    Call CreateMask(appRef)

    ' Create the kayers
    i = 1
    For i = 1 To 3

        docRef.ArtLayers(i).Duplicate docRef.ActiveLayer, 3

    Next 

    ' Label the layers x
    i = 1
    For i = 1 To 4

        docRef.ArtLayers(i).Name = "x"

    Next

    ' Create group; name; add mask
    docRef.LayerSets.Add
    docRef.LayerSets(1).Name = "x"
    Call CreateMask(appRef)

    ' Move the layers into the group
    i = 1
    For i = 1 To 3

        docRef.ArtLayers(1).Move docRef.LayerSets(1), 1

    Next

    ' Set the foreground color the black
    appRef.ForegroundColor.RGB.Red = 0
    appRef.ForegroundColor.RGB.Green = 0
    appRef.ForegroundColor.RGB.Blue = 0

    ' Purge everything including history
    appRef.Purge(4)


    ' //////////////////// Reset Photoshop preferences

    ' Beep when finished
    appRef.Preferences.BeepWhenDone = False

    ' Turn off tool tips
    appRef.Preferences.ShowToolTips = True
    
    ' Display only error dialogs
    appRef.DisplayDialogs = 1

    ' ////////////////////

End Sub


Sub CreateMask(appRef)

    DIM dialogMode
    dialogMode = 3
    DIM idMk
    idMk = appRef.CharIDToTypeID( "Mk  " )
        DIM desc20
        SET desc20 = CreateObject( "Photoshop.ActionDescriptor" )
        DIM idNw
        idNw = appRef.CharIDToTypeID( "Nw  " )
        DIM idChnl
        idChnl = appRef.CharIDToTypeID( "Chnl" )
        Call desc20.PutClass( idNw, idChnl )
        DIM idAt
        idAt = appRef.CharIDToTypeID( "At  " )
            DIM ref7
            SET ref7 = CreateObject( "Photoshop.ActionReference" )
            'DIM idChnl
            idChnl = appRef.CharIDToTypeID( "Chnl" )
            'DIM idChnl
            idChnl = appRef.CharIDToTypeID( "Chnl" )
            DIM idMsk
            idMsk = appRef.CharIDToTypeID( "Msk " )
            Call ref7.PutEnumerated( idChnl, idChnl, idMsk )
        Call desc20.PutReference( idAt, ref7 )
        DIM idUsng
        idUsng = appRef.CharIDToTypeID( "Usng" )
        DIM idUsrM
        idUsrM = appRef.CharIDToTypeID( "UsrM" )
        DIM idRvlA
        idRvlA = appRef.CharIDToTypeID( "RvlA" )
        Call desc20.PutEnumerated( idUsng, idUsrM, idRvlA )
    Call appRef.ExecuteAction( idMk, desc20, dialogMode )

End Sub

Sub SelectMask(appRef)

    DIM dialogMode
    dialogMode = 3
    DIM idslct
    idslct = appRef.CharIDToTypeID( "slct" )
        DIM desc586
        SET desc586 = CreateObject( "Photoshop.ActionDescriptor" )
        DIM idnull
        idnull = appRef.CharIDToTypeID( "null" )
            DIM ref377
            SET ref377 = CreateObject( "Photoshop.ActionReference" )
            DIM idChnl
            idChnl = appRef.CharIDToTypeID( "Chnl" )
            idChnl = appRef.CharIDToTypeID( "Chnl" )
            DIM idMsk
            idMsk = appRef.CharIDToTypeID( "Msk " )
            Call ref377.PutEnumerated( idChnl, idChnl, idMsk )
        Call desc586.PutReference( idnull, ref377 )
        DIM idMkVs
        idMkVs = appRef.CharIDToTypeID( "MkVs" )
        Call desc586.PutBoolean( idMkVs, False )
    Call appRef.ExecuteAction( idslct, desc586, dialogMode )

End Sub

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 14, 2020 0
Most Valuable Participant ,
Aug 14, 2020

Copy link to clipboard

Copied

If you are young and are going to share Photoshop scripts you should switch to JavaScript for its supported on mac and pc.  There just a few differences between Windows and Mac that script may need to consider. Most script I have seen work on both Mac and PC without any code to test which platform is running the script. The differences seem to be mostly related the the differences between Mac and PC File Systms and mainly only the difference in file filtering. 

 

For example selecting and filtering image file. File system dialog filtering  differ.  A function for both p]latforms may look like this.

function selectFile (multi) {   
	if (multi == true) {var theString = "please select files"}   
	else {var theString = "please select one file"};   
	if ($.os.search(/windows/i) != -1) {var theFiles = File.openDialog (theString, '*.nef;*.cr2;*.crw;*.dcs;*.raf;*.arw;*.orf;*.dng;*.psd;*.tif;*.tiff;*.jpg;*.jpe;*.jpeg;*.png;*.bmp', multi)}   
	else {var theFiles = File.openDialog (theString, getFiles, multi)};   
	////// filter files  for mac //////   
	function getFiles (theFile) {   
		if (theFile.name.match(/\.(nef|cr2|crw|dcs|raf|arw|orf|dng|psd|tif|tiff|jpg|jpe|jpeg|png|bmp)$/i) || theFile.constructor.name == "Folder") {  
			return true   
			};   
	};   
	return theFiles   
};			

 

JJMack

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 14, 2020 0
New Here ,
Aug 14, 2020

Copy link to clipboard

Copied

True JJMack - didn't think about the cross-platform part. Though the scripts are mainly for myself in my small business on Windows so probably doesn't matter.

 

Now I have to see how easy it is to convert my existing VBA scripts into Javascript! Did you learn JS? If you did, do you find it similar for PS? 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 14, 2020 0
Most Valuable Participant ,
Aug 14, 2020

Copy link to clipboard

Copied

While I can hack at Photoshop Scripting.  I actually do not know JavaScript.  I have never studied it or tried to learn it well.  I have a programming background and have worked on many operating system from the system side as well as the application side.  I never went to school to learn to be a programmer or learn a programing language.   One day my manager took my tool bag from my hands  and gave me a Green Card,  a book Basic Assembler Language and IBM 360 Princical of Operations,  and told me John your now a programmer.   For the next 38  years that was my  job title.  There is more to programming then grinding out code.  Thought I was a programmer for nearly 40 years I did not write that many programs.  I did a lot of design work. debugging, testing.  Mostly I designed and implemented development tools. That other used to develop  and test applications while I manage and maintained Lab machine rooms.  I would not say I was a great coder.  I can not type and make too many mistakes.  I'm always in error recovery mode.  Perhaps  that is way I was a useful designer, debugger, tester and could get things to work. 

 

I'm retired  nearly 80 and just hack a little to keep my mind from falling asleep forever.  Photoshop is my retirement toy.

JJMack

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 14, 2020 0
behiveco LATEST
New Here ,
Aug 16, 2020

Copy link to clipboard

Copied

That's a really nice story JJMack. Love that traditional way of learning - i.e. where you learn yourself outside of official college. That's what I did also in my work - homeschooling helped too. Pretty impressive your career also. Greatful for the help.

 

Best wishes, David.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 16, 2020 0