Scripting nconvert with autohotkey

Discussions on NConvert - the command line tool for image conversion and manipulation

Moderators: XnTriq, helmut, xnview

User avatar
Brother Gabriel-Marie
Posts: 286
Joined: Thu Aug 23, 2007 2:33 pm
Location: United States

Scripting nconvert with autohotkey

Post by Brother Gabriel-Marie »

Hi, y'all. I couldn't decide which forum to use!

I am trying to run an image through nconvert using autohotkey. My problem is that I need the output directory to be different than the input directory.

Here is my parameters line.
I've checked all the paths that they are correct in the script.

Do this look right to y'all?

Code: Select all

parameters := InputFileName . " -ratio -rtype lanczos -rflag orient -resize 700 540 -dpi 72 -out jpeg -o c:\test.jpg"
My problem is that although the file does get nconverted, the new file is outputted to the source directory instead of the output directory that I specified.
User avatar
Brother Gabriel-Marie
Posts: 286
Joined: Thu Aug 23, 2007 2:33 pm
Location: United States

Re: Scripting nconvert with ahk

Post by Brother Gabriel-Marie »

Ah, I figured it out:

parameters := "-ratio -rtype lanczos -rflag orient -resize 700 540 -dpi 72 -out jpeg -o c:\test.jpg " . InputFileName

The input filename has to go at the very end, enquoted if there are spaces.
cday
XnThusiast
Posts: 4215
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Scripting nconvert with ahk

Post by cday »

Brother Gabriel-Marie wrote:
The input filename has to go at the very end, enquoted if there are spaces.
The input file or files go at the end, something often missed, but in my experience placing quotes around input file names when there are spaces in the path doesn't work, but unquoted input files can be run successfully when there are spaces in the *filename* provided there are no spaces in the *path*. No problem using quotes around output paths and filenames with spaces.

I don't think that's documented anywhere but I've mentioned it before and not had any confirmation or explanation.

Edit:

I normally run NConvert in a Windows batch file .bat, I don't know if that is a factor?
User avatar
Brother Gabriel-Marie
Posts: 286
Joined: Thu Aug 23, 2007 2:33 pm
Location: United States

Re: Scripting nconvert with ahk

Post by Brother Gabriel-Marie »

Actually, in my script I am enquoting all the file paths and the process works just fine. But then again, I am scripting in autohotkey, not batch. I'm using AutoHotkey 1.1.11.01 on Windows 7 x64.

Here is a working Autohotkey script. Just save it as, oh, say, nconvert.ahk, be sure and set your outputDir path and then drop files on it.

Code: Select all

;Drag and drop a handful of images onto the script itself
;by BGM Wednesday, October 23, 2013
#noenv
nconvert := "C:\Program Files (x86)\XnView\nconvert.exe"  ;this may be different on your computer
outputDir := "c:\some folder\some other folder\"     ;make sure there is a trailing backslash...

loop, %0% 
{
    GivenPath := %A_Index%  ; Fetch the contents of the variable whose name is contained in A_Index.
    Loop %GivenPath%, 1 
    {
        RunNC(A_LoopFileLongPath)
	}
}
msgbox, Okay Bob!  All Done!
;-------------------------------------------------------------------

RunNC(whatfile){
	global
	splitpath, whatfile,,,,filenameonly  ;fetch just the name without the extension
	outputfile := chr(34) . outputDir . filenameonly . ".jpg" . chr(34)
	inputfile := chr(34) . whatfile . chr(34)

        ;notice that the full input path goes enquoted at the end.
        ;the output file is built from the original file name, and it gets enquoted, too
	parameters := "-ratio -rtype lanczos -resize 700 525 -dpi 72 -keepfiledate -overwrite -out jpeg -o " . outputfile . " " . inputfile
	msgbox %parameters%
        run, %nconvert% %parameters%,,hide   ;we want to hide all those cmd boxes
}
;-------------------------------------------------------------------
cday
XnThusiast
Posts: 4215
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Scripting nconvert with ahk

Post by cday »

Thanks for your explanation: I noticed that you were using autohotkey and the position of your quotes, but didn't realise that using autohotkey could affect the usual nconvert syntax.

Using autohotkey to run NConvert seems to be a first: searching the forum the only hits for 'nconvert' and 'autohotkey' are your posts. Do you see any advantage in running a script repetitively that way, rather than more conventionally in a Windows batch file?

I need to amend my comment on using NConvert with spaces in the input file path as I didn't recall the issue correctly:

Correction.

In my experience placing quotes around an input file name when there are spaces in the path *and a wildcard is used in the input filename* doesn't work, which can be a problem when multiple input files are to be processed. There seems to be a workaround, however, that can be used to avoid the problem: an input filename containing a wildcard, such as *.jpg , seems to work *without quotes* provided there are *no spaces in the input file path*.

I hope I've recalled the problem correctly this time. It's an issue I've encountered multiple times that has cost me time, but as far as I know it hasn't been confirmed and isn't documented anywhere.

There have been multiple posts about problems with spaces in file names, an issue that is generally viewed as a bug. I believe that it may be an issue to do with the expansion of wildcards, which apparently isn't supported in DOS or Windows cmd.exe and so has to be provided by the application, in which case it is perhaps better viewed as a limitation rather than a bug. Can anyone more knowledgeable shed any light on this?
User avatar
Brother Gabriel-Marie
Posts: 286
Joined: Thu Aug 23, 2007 2:33 pm
Location: United States

Re: Scripting nconvert with ahk

Post by Brother Gabriel-Marie »

Well, with Autohotkey, you can use ahk's own methods of managing strings and files. You can use the scripting system itself to build filenames - you don't have to rely on what is built into nconvert. You also don't have to mess with the batch language at all.

Another thing you can do is with ahk you can use runwait instead of run to call upon nconvert. Then, if you have some other program that needs the newly converted image, with runwait, you can be assured that nconvert has done its job and is finished with the image - that is, that ahk script will wait for nconvert to close before continuing. This could save some automation issues.

In general, as a scripting system, authotkey can do do all that batch can do and more. I try to use ahk scripting whenever possible on my computer.
cday
XnThusiast
Posts: 4215
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Scripting nconvert with ahk

Post by cday »

Looks interesting -- I didn't realise that ahk had those sort of capabilities -- in my very brief acquaintance with it some years back it seemed primarily a means of automating routine tasks by simulating keystrokes and mouse clicks.

There's evidently a lot more to it than that, but it looks like there is quite a lot to learn to harness those sort of capabilities.
User avatar
Brother Gabriel-Marie
Posts: 286
Joined: Thu Aug 23, 2007 2:33 pm
Location: United States

Re: Scripting nconvert with autohotkey

Post by Brother Gabriel-Marie »

Hey, you should give it a try. You can use the script above real easily. Just install ahk and then you can drag and drop onto the script just like you would a batch file.

There are thousands of examples of ahk scripts, and you don't need to know much to use the basics of it. It comes with an extensive offline help file, and the forum is active. There are a lot of ahk question also on Stack Exchange. You should give it a try.
CadFather
Posts: 2
Joined: Sun Mar 30, 2014 2:29 pm

Re: Scripting nconvert with autohotkey

Post by CadFather »

Hi, i'm searching for a way to integrate nconvert into the windows context menu (using filemenutools).

it seems this solution is the best i've seen so far, but my aim is to convert the images in 2 different folders (2 scripts really).

one conversion would output the files in the same folder as the original images.

the other one would output them in a subfolder where the original images are.

i tried playing around with the script (though i have no clue what i am doing!)

would someone be able to point me in the right direction?

Thank you
Max
CadFather
Posts: 2
Joined: Sun Mar 30, 2014 2:29 pm

Re: Scripting nconvert with autohotkey

Post by CadFather »

No one knows?