tag:blogger.com,1999:blog-47960728116220366392024-02-08T06:41:25.933-08:003EXWare English Blog - Some articles about REALbasicZhu Jia'nanhttp://www.blogger.com/profile/06727692368776733214noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-4796072811622036639.post-28778019216771295192009-01-09T07:36:00.000-08:002009-02-06T05:42:49.988-08:00Is a specific app running?<span class="Apple-style-span" style="font-family:arial;">How to know whether an app is running in REALbasic? The common idea is looping through all processes and testing with the name of the app which you want to know if it's opened.</span><br /><br /><span style="font-weight:bold;">Windows</span><br /><span class="Apple-style-span" style="font-family:arial;">On Windows, we can do it via declares, the Win32 APIs we need to use are EnumProcesses, EnumProcessModules, GetModuleFileNameEx, etc. I wrote a function a few days ago. You can find the source code of it in <span class="Apple-style-span" style="font-style: italic;">Window1</span> of the RB project attached. I didn't implemented it in a very smart way. It gets the names of all processes, then tests to see if a specific app name can be found. It because I wanted to demonstrate how to list the paths of all processes. You may make some changes on it to exit the For loop when the app is found.</span><br /><br /><span style="font-weight:bold;">Mac OS X</span><br /><span class="Apple-style-span" style="font-family:arial;">On Mac, calling AppleScript functions seems the best way. You can loop through all process objects provided by the System Events application in a tell block. However, since AppleScript supports using keyword <span style="font-style:italic;"><span class="Apple-style-span" style="">each</span></span> or plural of class name to access all objects in the direct parameter of <span style="font-style:italic;"><span class="Apple-style-span" style="">tell</span></span> statement (in this case, it's the System Events application), so that the script can be simply written like this:</span><br /><div><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">on run {appname}</span></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;"> </span></span></span><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">tell application "System Events"</span></span></div><div><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style=" ;font-family:Georgia;"><span class="Apple-tab-span" style="white-space: pre; "><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;"> </span></span></span><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">if (name of each process) contains appname then</span></span></span><span class="Apple-style-span" style="font-size:x-small;"><br /></span></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;"> </span></span></span><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">return true</span></span><span class="Apple-style-span" style="font-size:x-small;"><br /></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;"> </span></span></span><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">else</span></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;"> </span></span></span><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">return false</span></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;"> </span></span></span><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">end if</span></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;"> </span></span></span><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">end tell</span></span></div><div><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">end run</span></span></div><span class="Apple-style-span" style="font-family:arial;">OR:</span><br /><div><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">on run {appname}</span></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;"> </span></span></span><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">tell application "System Events"</span></span></div><div><span class="Apple-style-span" style=" white-space: pre;font-family:verdana;"><span class="Apple-style-span" style=" white-space: normal; font-family:Georgia;"><span class="Apple-tab-span" style="white-space: pre; "><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;"> </span></span></span><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">if (name of processes) contains appname then</span></span></span><span class="Apple-style-span" style="font-size:x-small;"><br /></span></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;"> </span></span></span><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">return true</span></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;"> </span></span></span><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">else</span></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;"> </span></span></span><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">return false</span></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;"> </span></span></span><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">end if</span></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;"> </span></span></span><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">end tell</span></span></div><div><span class="Apple-style-span" style=" ;font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">end run</span></span></div><div><br /></div><div><span class="Apple-style-span" style="font-family:arial;">On Leopard, AppleScript is updated to 2.0, many new features are introduced. As the result, it's able to test whether an app is running without launching it:</span></div><div><div><span class="Apple-style-span" style="font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">on run {appname}</span></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style="font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;"> </span></span></span><span class="Apple-style-span" style="font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">if application appname is running then</span></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style="font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;"> </span></span></span><span class="Apple-style-span" style="font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">return true</span></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style="font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;"> </span></span></span><span class="Apple-style-span" style="font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">else</span></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style="font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;"> </span></span></span><span class="Apple-style-span" style="font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">return false</span></span></div><div><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-style-span" style="font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;"> </span></span></span><span class="Apple-style-span" style="font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">end if</span></span></div><div><span class="Apple-style-span" style="font-family:verdana;"><span class="Apple-style-span" style="font-size:x-small;">end run</span></span></div><div><span class="Apple-style-span" style="font-family:arial;">The code above doesn't work on Tiger and earlier.</span></div><div><br /></div><div><span class="Apple-style-span" style="font-family:arial;">To use an AppleScript, drag and drop the scpt file to your project, and call it by its name showed in the listbox of project panel, as calling a global function. The AppleScript will be compiled and embedded to the app file you built. You will observe that the return value of any AppleScript you get in RB is a String, rather than the date type you expected. So you should compare it with "true" or "false", not a Boolean constant. Refer to the rbp I attached if my description isn't clear to you.</span></div><div><span class="Apple-style-span" style="font-family:arial;"><br /></span></div><div><span class="Apple-style-span" style="font-family:arial;"><span class="Apple-style-span" style="font-weight: bold;">Attachment:</span> <a href="http://www.3exware.com/online/IsProcRunning.zip">IsProcRunning.zip</a></span></div></div>Zhu Jia'nanhttp://www.blogger.com/profile/06727692368776733214noreply@blogger.com0tag:blogger.com,1999:blog-4796072811622036639.post-79503339542014174872009-01-08T07:30:00.000-08:002009-01-08T07:42:06.357-08:00Some compression arithmetics implemented in RB.I implemented some common compression arithmetics (LZ77, RLE and Huffman) in RB above a half year ago. I've referred to several open-source C/C++ projects/libraries. None of them has any limit of using, modification and issuance, so they can be used in any project for free.<br />Download them <a href="http://www.3exware.com/online/Compression.zip">here</a>.Zhu Jia'nanhttp://www.blogger.com/profile/06727692368776733214noreply@blogger.com0tag:blogger.com,1999:blog-4796072811622036639.post-2683469345311020892009-01-01T13:37:00.000-08:002009-02-06T05:40:51.186-08:00How to hide taskbar button of a window?In REALbasic, some types of window show buttons on taskbar in default. However, you may want to hide the taskbar button in some cases. For example, you want to use a document window as a modeless dialog, or you want to use a plain box as a splash window or a customized tooptip/popup window (not like REALbasic, most Windows applications won't display buttons on taskbar for their splash windows, such as Word, Windows Live Mail or RealPlayer). Simply add the follow code to the Open event handle of the window.<div><br /></div><div><div><span class="Apple-style-span" style="font-size: small;">#if TargetWin32 Then</span></div><div><span class="Apple-style-span" style=" ;"><span class="Apple-style-span" style="font-size: small;"> Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Int32, ByVal nIndex As Int32, ByVal dwNewLong As Int32) As Int32</span></span><span class="Apple-style-span" style="font-size: small;"><br /></span></div><div><span class="Apple-style-span" style="font-size: small;"> Const GWL_EXSTYLE = -20</span></div><div><span class="Apple-style-span" style="font-size: small;"> Const WS_EX_TOOLWINDOW = &H80</span></div><div><span class="Apple-style-span" style="font-size: small;"> Call SetWindowLong(Self.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW)</span></div><div><span class="Apple-style-span" style="font-size: small;">#endif</span></div></div>Zhu Jia'nanhttp://www.blogger.com/profile/06727692368776733214noreply@blogger.com3tag:blogger.com,1999:blog-4796072811622036639.post-74756320041121460732008-12-30T22:06:00.000-08:002009-01-03T03:57:12.842-08:00Setting the position of the mouse and so on...From time to time, I see some people ask how to set the mouse position, how to simulate mouse click, and something like these. I have written a sample for Windows and Linux (by calling Win32 and XLib APIs) about two years ago, and it still works fine with the last REALbasic release as I just tested. Click <a href="http://www.3exware.com/online/FakeMouse.zip">here</a> to download it.Zhu Jia'nanhttp://www.blogger.com/profile/06727692368776733214noreply@blogger.com0