Jump to content

Multiple icons in single compiled scriptfile?


Recommended Posts

Hi,

I'd like to distribute a compiled script as a single .exe file that sits in the system tray and when specific conditions are met, changes the tray icon to give visual feedback to the user.

I can change the default icon (option to use ResHack on compile), and I've found TraySetIcon in the help, but this needs an icon filename to be given.

I've seen that other compiled windows programs can contain multiple icons - and I suspect that using ResHack I could add additional icons to my compiled scripts. But how then can I switch between them?

I can think of two ways:

1) use the windows API to "load the icon resource" and "switch the tray icon"

2) convert the icon to a hex constant inside my script, create an .ICO file on initialization, then use TraySetIcon

Is there any other way?

MisterBates

Link to comment
Share on other sites

Perfect!

I hadn't thought that once the icons were inside the compiled script, they were accessible to TraySetIcon (duh!)

And inclusion of the code/method you use to compile was a fantastic bonus.

Thanks!

MisterBates

Link to comment
Share on other sites

Got it all working - thanks to all!

I have an .rc file with the icons to be added, numbered from 200 so they come after the default icons:

CODE

; Icon resources to go into compiled file

200 ICON "Suspend Screen Saver - Active.ico"

201 ICON "Suspend Screen Saver - Suspended.ico"

I have a batch file that I've set to run after the compile (third tab in compile dialog - I had to put the filename inside "" to have spaces in the filename of the .bat file):

CODE

@echo off

: Post-compile SciTE file to add multiple icons to compiled AutoIt script

: ------

set StepName=Setup environment

echo === %StepName%

setlocal

: CUSTOMIZE: Next two lines need modifying for each different script

: ================================================

set ScriptDir=C:\Documents and Settings\batemans\My Documents\_Stuff\My Programs\Suspend ScreenSaver

set ScriptName=Suspend Screen Saver

: ================================================

: AutoIt (and other tools)

set AutoItDir=C:\Program Files\AutoIt3

set RCName=%AutoItDir%\Go Tools\GoRC.exe

set UPXName=%AutoItDir%\Aut2Exe\upx.exe

set ResHack=%AutoItDir%\SciTE\AutoIt3Wrapper\ResHacker.exe

: Change working directory

CD "%ScriptDir%"

: ------

set StepName=Compile RC file

echo === %StepName%

if exist "%ScriptName%.res" del "%ScriptName%.res"

"%RCName%" /r "%ScriptName%.rc"

if not errorlevel 0 goto ReportErr

if not exist "%ScriptName%.res" goto ReportErr

: ------

set StepName=Uncompress compiled .exe file

echo === %StepName%

"%UPXName%" -d "%ScriptName%.exe"

if not errorlevel 0 goto ReportErr

: ------

set StepName=Add icons to .exe

echo === %StepName%

"%ResHack%" -add %ScriptName%.exe, %ScriptName%.exe, %ScriptName%.res ,,,

if not errorlevel 0 goto ReportErr

: ------

set StepName=Compress compiled .exe file

echo === %StepName%

"%UPXName%" --compress-icons=0 "%ScriptName%.exe"

if not errorlevel 0 goto ReportErr

goto End

:ReportErr

echo Error running post-compile .bat file - Errorlevel %errorlevel% in step: %StepName%

pause

:End

cd .

And in my Script, I setup the icons to be used as CONSTs, then switch icons depending on the CONSTs:

Const $Icon_Default = 3 ; Number of icons AutoIt adds to .exe
if @Compiled Then ; Icons are added to .EXE file
  Const $IconActive = $Icon_Default
  Const $IconSuspended = $IconActive + 1
Else
  Const $IconActive = @ScriptDir & "\" & $ScriptName & " - Active.ico"
  Const $IconSuspended = @ScriptDir & "\" & $ScriptName & " - Suspended.ico"
EndIf

...

if IsNumber($IconActive) then
  TraySetIcon(@ScriptFullPath, $IconActive)
Else
  TraySetIcon($IconActive)
EndIf

Perfect!

Thanks to Hubertus72 and Zedna

Link to comment
Share on other sites

Zedna,

Thanks for the pointer, and once these enhancements are in general release, I'll convert my code. They look great!

Couple final things to add about the solution I documented above:

1) I didn't have the microsoft resource compiler RC.EXE to compile my icons into resource format, so I used the excellent freeware compiler GORC.EXE available at http://www.jorgon.freeserve.co.uk/

2) My post-compile batch file has spaces in the filename, which casues a problem for SciTE (?). I thought I could fix it by surrounding the batch file name with double quotes - but SciTE strips these off each compile! So I ended up surrounding the batch file name with double quotes, and putting 'CMD.EXE /C ' at the front, which runs a copy of the command prompt, executes the batch file (apssed as a double-quoted filename) then exits the command prompt. Output is still captured by SciTE into the compile window!

MisterBates

Edited by MisterBates
Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...