Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    94
  • comments
    127
  • views
    83648

Simple context-menu extension. Part 2 - batching

Sign in to follow this  
Seriema

353 views

Updated 2006-02-16: In step 10, it should be 85%% and not 85%. % works as the escape character \, if you want that actual character you'll need two.

What?
In my last journal entry I wrote how to add a menu item in the context menu that calls a commandline tool and passes some arguments. Today I'll show you how to do this for all specified file types in a folder! If you want a fast read just skip the Trivia-boxes(TM).

What's different?
Just follow the steps from the last post, with 2 modifications.
1) Instead of choosing JPG as the file type choose Folder instead. Folder should be among the first types.
2) The menu text and the execution line differs, see the end of this article.

Quote:
Trivia-box(TM) - feel free to ignore this
The line we want to run is based on something I cooked up in the command prompt: FOR %x IN (*.jpg) DO c:\wherever\convert.exe %x -quality 85%% %x
But if you try that you'll notice that Windows won't accept it, because the first word "FOR" isn't a executable file path. Gah!

Eureka!
We call the command prompt and pass it that line as a string! *grins* That gives us two options, to leave the command prompt open after the compression or not. I left it behind while trying this out, but for normal use I prefer it to go away. The difference is the /param passed to cmd.exe.
/c closes the window after execution
/k keeps the window open after execution

By adding cmd.exe /c before the command line above into the command prompt we are shocked to notice that the console closes directly (even with the /k switch). This is because %variable names are reserved for system variables in this working area, what we want is %%variable (or rather, %%x). You can read about this by typing FOR /? in the command prompt. Also, inside the FOR-loop we'll now want to use the counter variable %%x and not the system variable %L.

Supporting JPEGs with different file extensions is easy as pi, just change the condition in the FOR-loop to (*.jpg *.jpeg *.jpe).


Quote:
Trivia-box(TM) - feel free to ignore this
I wanted the menu item to only show up if there's actually a JPEG in the folder. When I found this MSDN article on context menus in Internet Explorer I thought I had found the answers. The flag for images is 0x2, but to set the flags we have to pull up the windows registry. The binary value that should be added is named Contexts and is in the root folder of our command, in my case:
HKEY_CLASSES_ROOT\Folder\shell\Batch_compress_JPG's_to_85%
which should hold the value 2. But this doesn't work! =( Anyone know how to make it work?


How?
Simply add this before your thought command line: cmd.exe /c FOR %%x IN (*.???) DO and change the *.??? to any file filter you want, such as .jpg. Use space to add more file types. Also, instead of the windows system variable %L use the FOR loops variable %%x.

If you followed my last article then here's the change to the last step.
The text line is:
"Batch compress JPEG's to 85%"
The execution line is:
cmd.exe /c FOR %%x IN (*.jpg *.jpeg *.jpe) DO "C:\wherever\ImageMagick\convert.exe" "%%x" -quality 85%% "%%x"
(Don't forget to change the file path to ImageMagick's convert.exe!)

End
You should have this now.



Quote:
BONUS Trivia-box(TM) - feel free to ignore this
While trying this out I found, to my surprise, that Windows wouldn't let me Edit an action added to a folder! So I went into the registry and found the path to where it adds the magic:
HKEY_CLASSES_ROOT\Folder\shell
where it adds a folder called
Batch_convert_JPGs_to_85%
with a defualt value of the menu item text, "Batch convert JPGs to 85%" and a subkey
HKEY_CLASSES_ROOT\Folder\shell\Batch_convert_JPGs_to_85%\command
with the default value of the execution line, "cmd.exe /c FOR %%x IN (*.jpg *.jpeg *.jpe) DO "C:\wherever\ImageMagick \convert.exe" "%%x" -quality 85%% "%%x""


Thanks goes out to muer, snk_kid, Washu and DigitalDelusion for proofreading this.
Sign in to follow this  


1 Comment


Recommended Comments

Guest Anonymous Poster

Posted

First of all, you're crazy!

Congratulations for your journal. Keep up the good work.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!