Applescript - how to compare (in an if-clause) a bundle identifier? - if-statement

Something like this script
tell application "Safari"
activate
end tell
tell application "System Events"
set frontAppID to bundle identifier of first process whose frontmost is true
end tell
returns "com.apple.Safari"
However, if I try something like
if frontAppID of bundle identifier of first process whose frontmost is "com.apple.safari" then
then it doesn't work. Obviously a bundle identifier cannot be compared with a string. How do I create a bundle identfier object I can use in my comparison?

The bundle identifier is a string, so a string-to-string comparison is fine. The problems with your statement are: frontAppID of bundle identifier, which isn't valid because frontAppID isn't a property of bundle identifier; and frontmost is should be followed by a boolean, not a string.
Fixing these issues (and assuming you'll enclose this within the appropriate System Events command block):
if the bundle identifier ¬
of (the first process ¬
whose frontmost is true) ¬
is "com.apple.safari" then ¬
return true
P.S. Are you aware you can also do this (without needing to call System Events):
if application "Safari" is frontmost then return true
or, using the bundle identifier:
if application id "com.apple.safari" is frontmost then return true

Your syntaxe is not correct. correct should be:
tell application "System Events"
if (bundle identifier of first process whose frontmost is true) is "com.apple.Safari" then
display dialog "Safari front most !"
end if
end tell

Related

RegisterEventSource(0, "App1.exe") => what if there are multiple "App1.exe"s under ...\Eventlog registry key?

I am trying to use the Windows Event Logging service and in struggle with RegisterEventSource owing to the insufficient/ambiguous description.
Let's assume "App1.exe" is the event source name that will be used from an application program App1.exe.
And I will use my own custom event log subkey named "MyCustLog" under which "App1.exe" subkey will be created.
So, my custom event log subkey for App1.exe will look like "... \MyCustLog\App1.exe" under which EventMessageFile/CategoryMessageFile/CategoryCount will be set as "App1.exe"/"App1.exe"/3, respectively.
Now, there happens and/or will happen to be another "App1.exe" event source name registered under a 3rd party custom event log key as shown below:
HKEY_LOCAL_MACHINE
SYSTEM
CurrentControlSet
Services
Eventlog
Application
Security
System
3rdPartyLog
App1.exe
After I create my own custom event log key it looks like:
HKEY_LOCAL_MACHINE
SYSTEM
CurrentControlSet
Services
Eventlog
Application
Security
System
3rdPartyLog
App1.exe
MyCustLog
App1.exe
Now, I call RegisterEventSource follows:
HANDLE hEvtSrc = RegisterEventSource(0, "App1.exe");
=> returns an event log handle to hEvtSrc, but how can I be sure that the handle is returned for MyCustLog's App1.exe, not 3rdPartyLog's App1.exe?
What I have tried:
I tried the following:
HANDLE hEvtSrc2 = RegisterEventSource(0, "MyCustLog\\App1.exe");
=> this also successfully returns an event log handle but not sure if it is for MyCustLog's App1.exe, nonetheless. Moreover, an event fired by the following ReportEvent with this handle shows failure description in its property window from Windows Event Log Viewer.
CONST LPCTSTR pBadCommand = _T("The command that was not valid");
DWORD dwEventDataSize = ((DWORD)_tcslen(pBadCommand) + 1) * sizeof(TCHAR);
BOOL bRet = ReportEvent(hEventLog, EVENTLOG_ERROR_TYPE, UI_CATEGORY, MSG_INVALID_COMMAND, NULL, 0, dwEventDataSize, NULL, (LPVOID)pBadCommand);
The failure message says: The description for event id MSG_INVALID_COMMAND cannot be found from the source MyCustLog\App1.exe
I also tried the third way:
HANDLE hEvtSrc3 = RegisterEventSource(0, "MyCustLog/App1.exe");
=> this again successfully returns an event log handle, but still an event fired by ReportEvent with this handle shows the almost the same failure description in its property window from event viewer. The only difference in failure message is MyCustLog/App1.exe instead of MyCustLog\App1.exe.
Therefore, "MyCustLog\App1.exe" or "MyCustLog/App1.exe" cannot be used instead of "App1.exe" to get the right handle of my own for RegisterEventSource. So we are back to the square one again.
So, once again, how can I be sure the returned handle for RegisterEventSource(0, "App1.exe") is for MyCustLog\App1.exe, not for 3rdPartyLog\App1.exe?
Below is shown the .mc file in use for App1.exe.
[Message file]
; // App1.mc
; // This is the header section.
SeverityNames=(Success=0x0:STATUS_SEVERITY_SUCCESS
Informational=0x1:STATUS_SEVERITY_INFORMATIONAL
Warning=0x2:STATUS_SEVERITY_WARNING
Error=0x3:STATUS_SEVERITY_ERROR
)
FacilityNames=(System=0x0:FACILITY_SYSTEM
Runtime=0x2:FACILITY_RUNTIME
Stubs=0x3:FACILITY_STUBS
Io=0x4:FACILITY_IO_ERROR_CODE
)
LanguageNames=(English=0x409:MSG00409)
; // The following are the categories of events.
MessageIdTypedef=WORD
MessageId=0x1
SymbolicName=NETWORK_CATEGORY
Language=English
Network Events
.
MessageId=0x2
SymbolicName=DATABASE_CATEGORY
Language=English
Database Events
.
MessageId=0x3
SymbolicName=UI_CATEGORY
Language=English
UI Events
.
; // The following are the message definitions.
MessageIdTypedef=DWORD
MessageId=0x100
Severity=Error
Facility=Runtime
SymbolicName=MSG_INVALID_COMMAND
Language=English
The command is not valid.
.
MessageId=0x101
Severity=Error
Facility=System
SymbolicName=MSG_BAD_FILE_CONTENTS
Language=English
File %1 contains content that is not valid.
.
MessageId=0x102
Severity=Warning
Facility=System
SymbolicName=MSG_RETRIES
Language=English
There have been %1 retries with %2 success! Disconnect from
the server and try again later.
.
MessageId=0x103
Severity=Informational
Facility=System
SymbolicName=MSG_COMPUTE_CONVERSION
Language=English
%1 %%4096 = %2 %%4097.
.
; // The following are the parameter strings */
MessageId=0x1000
Severity=Success
Facility=System
SymbolicName=QUARTS_UNITS
Language=English
quarts%0
.
MessageId=0x1001
Severity=Success
Facility=System
SymbolicName=GALLONS_UNITS
Language=English
gallons%0
.
If there are multiple App1.exe event sources, there is no way to tell RegisterEventSource() which source to use. RegisterEventSource() only accepts a source name, not a log name.
In fact, the Event Sources documentation says:
You cannot use a source name that has already been used as a log name. In addition, source names cannot be hierarchical; that is, they cannot contain the backslash character ("").
Which is why registering MyCustLog\App1.exe doesn't work, as it is an illegal source name. And why registering MyCustLog/App1.exe doesn't differentiate App1.exe between multiple logs, as / is not a hierarchy delimiter so RegisterEventSource() treats MyCustLog/App1.exe as the entire source name, not as App1.exe underneath MyCustLog.
So, since the source name for both logs is just App1.exe, you must use RegisterEventSource("App1.exe"), and so it will use the the first App1.exe it finds. And if it doesn't find any, it will fallback to using the Application log, which doesn't support category/message files.
So, make sure your event source is named uniquely. For instance, by NOT using your app's filename, but rather more descriptive names, eg:
HKEY_LOCAL_MACHINE
SYSTEM
CurrentControlSet
Services
Eventlog
My Custom Log
My Custom App Name
You can point My Custom App Name to the actual App1.exe file in its EventMessageFile/CategoryMessageFile values as needed.
And then you can use RegisterEventSource("My Custom App Name").
You should have a look at your local Registry to see how other apps register their event sources. It is actually rare for filenames to be used as key names. For example, here is what is registered in the eventlog key on my local system. As you can see, none of the log keys or app keys use filenames for their names:

apex_application.g_print_success_message variable does not get set

On my DML form I have a delete process that calls a PL/SQL function:
DECLARE
err_code NUMBER;
BEGIN
my_package.delete_record(id => :P2_ID,error_code => err_code);
IF NVL(TO_NUMBER(err_code),0) = 0 THEN
apex_debug.message('Record deleted');
apex_application.g_print_success_message := 'Record deleted';
END IF;
END;
Somehow apex_application.g_print_success_message does not get set and displayed even though everything else works as I see a debug message being written. Can anyone help my figure this one out? Could it be because I have multiple processes on the page?
Check for the process point of yout page process, the success message won't appear on a "Before Header" process point.
If that doesn't help, check out the apex.message API.
apex.message.showPageSuccess( "Record deleted" );
Only the last assignment are shown.
So check if you have an empty assignment to it somewhere else.
apex_application.g_print_success_message:='';
Also see if you by any chance have filled in a space in one of the page processes "Success Message"
Updating that variable directly is not supported.
You can define a hidden item, set that within your processes
:P0_MY_HIDDEN_ITEM := 'Process outcome';
Then include the following in the success message attribute to your final process.
Errors can be added using apex_error package.

How to test GenServer restart behaviour?

In my app I have a GenServer. It backs up data needed to start again in an Agent. I want to test if my GenServer backs up and restores correctly, so I wanted to start backup agent, then restart GenServer and see if it works (remembers config from before restart).
Right now I have GenServer configured and started (with start_supervised!) in test setup. I need to somehow restart that GenServer.
Is there a good way to do it? Should I be doing it completely differently? Is there a different, correct way of testing restart behavior?
A Supervisor decides when to restart a process under its supervision through the child_spec of that child process. By default, when you define your GenServer module, and use use GenServer on the module, the default (restart values) will be :permanent, which means, always restart this process if it exits.
Given this, it should be enough to send it an exit signal, with Process.exit(your_gen_server_pid, :kill) (:kill will ensure that even if the process is trapping exits it will be killed), and the supervisor should then start the process again and you can then do your assertions.
You'll need a way to address the "new" genserver process, since it will be killed, when restarted its pid won't be the same as was originally, usually you do that by providing a name when starting it.
If your genserver loads the state as part of its init you don't necessarily need to supervise it to test the backup behaviour, you could just start it individually, kill it, and then start it again.
There might be edge-cases depending on how you establish the backup, etc, but normally that would be enough.
UPDATE:
To address both the process exiting and being up again, you could write 2 helper functions to deal specifically with that.
def ensure_exited(pid, timeout \\ 1_000) do
true = Process.alive?(pid)
ref = Process.monitor(pid)
Process.exit(pid, :kill)
receive do
{:DOWN, ^ref, :process, ^pid, _reason} -> :ok
after
timeout -> :timeout
end
end
You could make it take instead a name and do GenServer.whereis to retrieve the pid, but the idea is the same.
To make sure it's alive:
def is_back_up?(name, max \\ 200, tries \\ 0) when tries <= max do
case GenServer.whereis(name) do
nil ->
Process.sleep(5)
is_back_up?(name, max, tries + 1)
pid -> true
end
end
def is_back_up?(_, _, _), do: false
The basic idea is that. Not sure if there's already some helpers to do this sort of thing.
Then you just use that (you could write a 3rd helper that takes the live pid, the name, and does it all in one "step"), or write:
:ok = ensure_exited(pid)
true = is_back_up?(name)

QResource: unregister .rcc files

We have an application with multiple themes, whom calls other minor apps. So, on main app there is something like that:
// User opens app with theme A
QResource::registerResource("theme_a.rcc"); // returns TRUE
// User changes theme to B
QResource::unregisterResource("theme_a.rcc"); // returns TRUE
QResource::registerResource("theme_b.rcc"); // returns TRUE
Everything works fine on main application. The problem begins when this software calls other qt apps.
Inside those minor apps we follow the same flow of register and unregister. The weird part is that register always works and unregister never works (only inside minors apps). It's happening something like that:
// User opens app with theme A
QResource::registerResource("minor_theme_a.rcc"); // returns TRUE
// User changes theme to B
QResource::unregisterResource("minor_theme_a.rcc"); // returns FALSE
QResource::registerResource("minor_theme_b.rcc"); // returns TRUE
Why is it happening? Is there a solution?
unregisterResource returns true if the resource is successfully unloaded and no references exist for the resource.
So in your case there could be still some more references from some other forms.
Important documentation with respect to unregisterResource:
If there are QResources that currently reference resources related to the unregistered file, they will continue to be valid but the resource file itself will be removed from the resource roots, and thus no further QResource can be created pointing into this resource data. The resource itself will be unmapped from memory when the last QResource that points to it is destroyed.
So my strong guess is some QResource is still pointing to the one that you tried to unregister.

QTP UFT - WinList.Select cannot select any line

I need a qtf script that selects a line and proceeds. Recording the steps gave the following code:
Window("[windowname]").WinList("ListBox").Select "[Name of the item] "
But when I try to run it I see an error box: Cannot identify the specific item of the ListBox object.
This does not work with any of the lines. I cannot even select one with the arrow down key, the error is always the same.
Please try to highlight WinList from object repository before you run the script. If the object cannot be found, using an updating function to update the object property.
Also, make sure that every time you are running your script for standard window, your screen doesn't minimize.