WScript.Shell Run gives "No application is associated with the specified file for this operation." when opening images. - silverlight-5.0

I am facing an issue while trying to open an image file from a Silverlight app in Windows 10 using WScript.Shell.
The code is as follows.
try
{
dynamic shell = AutomationFactory.CreateObject("WScript.Shell");
shell.Run(#"C:\temp\X.jpg");
}
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace);
}
This piece of code works perfectly fine when the default application is set to 'Photos' / 'Internet Explorer' in Windows 10 'Default Apps' settings.
However, when the default app is set to 'Paint', I get an exception
"No application is associated with the specified file for this operation. (Exception from HRESULT: 0x80070483)"
Please note that when I try to double click on the same image in Windows explorer, it opens up in Paint application without errors.
Why does this happen?
Please help.

Problem
windows script host not running desired application for known file type
Solution
expressly specify the desired application and run using WScript.Shell Object
Example
in the example below, we reference both the program and the file we want to run with strRunLine
<?xml version="1.0"?>
<package>
<job id="default">
<script language="jscript">
<![CDATA[
// save to demo_runpaint.wsf
var WshShell = new ActiveXObject("Wscript.Shell");
var strRunLine = "";
strRunLine = "mspaint.exe C:/path/to/capture.png";
WshShell.Run( strRunLine );
]]>
</script>
</job>
</package>
Pitfalls
the desired program must be in your system PATH or else you must specify the full path to the application expressly.

Related

How to run AppleScript from C++ in macOS sandbox environment without entitlement violations

I am trying to use AppleScript to control the Apple Photos app from within a QT C++ app that must run in the sandbox environment required for the macOS App Store.
I have tried to run the AppleScript via a QProcess launching osascript, like this :
const QString aScript = QString(
"tell application \"Photos\"\n"
" set selMedia to (get media items whose id contains \"%1\")\n"
" if not (album \"Trash from %2\" exists) then\n"
" make new album named \"Trash from %2\"\n"
" end if\n"
" add selMedia to album \"Trash from %2\"\n"
"end tell").arg(fileNameNoExt, APP_NAME);
QProcess script;
script.setProgram("osascript");
script.setArguments(QStringList() <<
"-e" << aScript);
script.start();
script.waitForFinished();
int exitCode = script.exitCode();
if(exitCode!=0){
QString outMsg = script.readAllStandardOutput();
QString errorOut = script.readAllStandardError();
// Warn user and output message...
}
This works fine outside the sandbox, adding the desired items from the library to the desired album. However, inside the sandbox, I get an errAEPrivilegeError. Looking in the Console for the error event, it states :
AppleEvents/sandbox: Returning errAEPrivilegeError/-10004 and denying dispatch of event core/getd from process '<private>'/0x0-0x4d84d8, pid=31654, because it is not entitled to send an AppleEvent to this process.
Here is my entitlements file when signing the app :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.scripting-targets</key>
<dict>
<key>com.apple.Photos</key>
<array>
<string>com.apple.Photos.library.read-write</string>
</array>
</dict>
</dict>
</plist>
When the app is first run (sandboxed) and the AppleScript is started, I get the system permission box, saying that the app is asking to control the Photos app. I click OK of course, but then I get the error. And upon next runs, as the permission is already granted, no system permission box comes up, but it fails nonetheless. When I reset the permissions with the terminal command tccutil reset All [my app bundle id], then upon the next execution of the app and the AppleScript, I get the permission box again.
If I delete the com.apple.security.scripting-targets entitlement, I get no system permission request and the error is different : from my app the error output states Photos isn't running, from the console it says it was denied due to sandboxing. If I keep the scripting-targets and com.apple.Photos but delete library.read-write I get the errAEPrivilegeError again.
I have tried to only test the make new album named "Trash [...]"but this still fails with errAEPrivilegeError. Even if only telling Photos to quit with AppleScript (which according to Photos.app/Contents/Resources/Photos.sdef doesn't require any specific permissions appart from scripting, I also get errAEPrivilegeError. The only command that seems to work is activate.
Also, if I try to run the qprocess not as a separate process, with QProcess::execute (if maybe the sandbox problem is because it is not the main app running the script ?), I still get the same errAEPrivilegeError.
What am I missing for it to work, or what other method should I use to run an applescript from C++/QT when in a sandbox ?
Did you include NSAppleEventsUsageDescription in your app’s Info.plist?
[ETA]
OK, after further digging I can confirm the behavior you’re seeing. With the correct app entitlements an in-process NSAppleScript can send AEs to Photos; however, the same process is unable to send AEs from a child osascript process.
According to Apple docs (which are lousy), a child process must have exactly 2 entitlements to inherit its parent’s: com.apple.security.app-sandbox and com.apple.security.inherit. However, running codesign --display --entitlements - /usr/bin/osascript shows osascript has neither of these but a bunch of unrelated entitlements. So this looks like Apple’s screwup in how they’ve built osascript.
When running AppleScripts that are part of a Swift/ObjC app, I generally recommend using the AppleScript-ObjC bridge to load and call your AppleScript handlers directly; no subprocess required. I have no idea how to do that in a Qt app though.
The simplest solution may be to write your own command-line executable/XPC service in ObjC which runs your AppleScript code via NSAppleScript, giving it the com.apple.security.app-sandbox and com.apple.security.inherit entitlements, and embed it in your Qt app’s main bundle.
(You may also need to add com.apple.security.automation.apple-events in the main app’s entitlements if the runtime is hardened.)
…
One more thing: avoid generating AppleScript code via string interpolation. If you want to parameterize a script, pack your values as parameters in an Apple event and call the corresponding AS handler via -executeAppleEvent:error:.

Open a c++ application installed on computer with a custom url in browser [duplicate]

How do i set up a custom protocol handler in chrome? Something like:
myprotocol://testfile
I would need this to send a request to http://example.com?query=testfile, then send the httpresponse to my extension.
The following method registers an application to a URI Scheme. So, you can use mycustproto: in your HTML code to trigger a local application. It works on a Google Chrome Version 51.0.2704.79 m (64-bit).
I mainly used this method for printing document silently without the print dialog popping up. The result is pretty good and is a seamless solution to integrate the external application with the browser.
HTML code (simple):
Click Me
HTML code (alternative):
<input id="DealerName" />
<button id="PrintBtn"></button>
$('#PrintBtn').on('click', function(event){
event.preventDefault();
window.location.href = 'mycustproto:dealer ' + $('#DealerName').val();
});
URI Scheme will look like this:
You can create the URI Scheme manually in registry, or run the "mycustproto.reg" file (see below).
HKEY_CURRENT_USER\Software\Classes
mycustproto
(Default) = "URL:MyCustProto Protocol"
URL Protocol = ""
DefaultIcon
(Default) = "myprogram.exe,1"
shell
open
command
(Default) = "C:\Program Files\MyProgram\myprogram.exe" "%1"
mycustproto.reg example:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Classes\mycustproto]
"URL Protocol"="\"\""
#="\"URL:MyCustProto Protocol\""
[HKEY_CURRENT_USER\Software\Classes\mycustproto\DefaultIcon]
#="\"mycustproto.exe,1\""
[HKEY_CURRENT_USER\Software\Classes\mycustproto\shell]
[HKEY_CURRENT_USER\Software\Classes\mycustproto\shell\open]
[HKEY_CURRENT_USER\Software\Classes\mycustproto\shell\open\command]
#="\"C:\\Program Files\\MyProgram\\myprogram.exe\" \"%1\""
C# console application - myprogram.exe:
using System;
using System.Collections.Generic;
using System.Text;
namespace myprogram
{
class Program
{
static string ProcessInput(string s)
{
// TODO Verify and validate the input
// string as appropriate for your application.
return s;
}
static void Main(string[] args)
{
Console.WriteLine("Raw command-line: \n\t" + Environment.CommandLine);
Console.WriteLine("\n\nArguments:\n");
foreach (string s in args)
{
Console.WriteLine("\t" + ProcessInput(s));
}
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey();
}
}
}
Try to run the program first to make sure the program has been placed in the correct path:
cmd> "C:\Program Files\MyProgram\myprogram.exe" "mycustproto:Hello World"
Click the link on your HTML page:
You will see a warning window popup for the first time.
To reset the external protocol handler setting in Chrome:
If you have ever accepted the custom protocol in Chrome and would like to reset the setting, do this (currently, there is no UI in Chrome to change the setting):
Edit "Local State" this file under this path:
C:\Users\Username\AppData\Local\Google\Chrome\User Data\
or Simply go to:
%USERPROFILE%\AppData\Local\Google\Chrome\User Data\
Then, search for this string: protocol_handler
You will see the custom protocol from there.
Note: Please close your Google Chrome before editing the file. Otherwise, the change you have made will be overwritten by Chrome.
Reference:
https://msdn.microsoft.com/en-us/library/aa767914(v=vs.85).aspx
Chrome 13 now supports the navigator.registerProtocolHandler API. For example,
navigator.registerProtocolHandler(
'web+custom', 'http://example.com/rph?q=%s', 'My App');
Note that your protocol name has to start with web+, with a few exceptions for common ones (like mailto, etc). For more details, see: http://updates.html5rocks.com/2011/06/Registering-a-custom-protocol-handler
This question is old now, but there's been a recent update to Chrome (at least where packaged apps are concerned)...
http://developer.chrome.com/apps/manifest/url_handlers
and
https://github.com/GoogleChrome/chrome-extensions-samples/blob/e716678b67fd30a5876a552b9665e9f847d6d84b/apps/samples/url-handler/README.md
It allows you to register a handler for a URL (as long as you own it). Sadly no myprotocol:// but at least you can do http://myprotocol.mysite.com and can create a webpage there that points people to the app in the app store.
This is how I did it. Your app would need to install a few reg keys on installation, then in any browser you can just link to foo:\anythingHere.txt and it will open your app and pass it that value.
This is not my code, just something I found on the web when searching the same question. Just change all "foo" in the text below to the protocol name you want and change the path to your exe as well.
(put this in to a text file as save as foo.reg on your desktop, then double click it to install the keys)
-----Below this line goes into the .reg file (NOT including this line)------
REGEDIT4
[HKEY_CLASSES_ROOT\foo]
#="URL:foo Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\foo\shell]
[HKEY_CLASSES_ROOT\foo\shell\open]
[HKEY_CLASSES_ROOT\foo\shell\open\command]
#="\"C:\\Program Files (x86)\\Notepad++\\notepad++.exe\" \"%1\""
Not sure whether this is the right place for my answer, but as I found very few helpful threads and this was one of them, I am posting my solution here.
Problem: I wanted Linux Mint 19.2 Cinnamon to open Evolution when clicking on mailto links in Chromium. Gmail was registered as default handler in chrome://settings/handlers and I could not choose any other handler.
Solution:
Use the xdg-settings in the console
xdg-settings set default-url-scheme-handler mailto org.gnome.Evolution.desktop
Solution was found here https://alt.os.linux.ubuntu.narkive.com/U3Gy7inF/kubuntu-mailto-links-in-chrome-doesn-t-open-evolution and adapted for my case.
I've found the solution by Jun Hsieh and MuffinMan generally works when it comes to clicking links on pages in Chrome or pasting into the URL bar, but it doesn't seem to work in a specific case of passing the string on the command line.
For example, both of the following commands open a blank Chrome window which then does nothing.
"c:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "foo://C:/test.txt"
"c:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --new-window "foo://C:/test.txt"
For comparison, feeding Chrome an http or https URL with either of these commands causes the web page to be opened.
This became apparent because one of our customers reported that clicking links for our product from a PDF being displayed within Adobe Reader fails to invoke our product when Chrome is the default browser. (It works fine with MSIE and Firefox as default, but not when either Chrome or Edge are default.)
I'm guessing that instead of just telling Windows to invoke the URL and letting Windows figure things out, the Adobe product is finding the default browser, which is Chrome in this case, and then passing the URL on the command line.
I'd be interested if anyone knows of Chrome security or other settings which might be relevant here so that Chrome will fully handle a protocol handler, even if it's provided via the command line. I've been looking but so far haven't found anything.
I've been testing this against Chrome 88.0.4324.182.
open
C:\Users\<Username>\AppData\Local\Google\Chrome\User Data\Default
open Preferences then search for excluded_schemes you will find it in 'protocol_handler' delete this excluded scheme(s) to reset chrome to open url with default application

What's the difference between programming in browser and WebStorm?

I was just writing some codes in WebStorm, just like below
var cities = {
resorts : ["new york", 'los angeles', 'san jose'],
print: (delay = 1000 ) => setTimeout(()=>{ console.log(this===window)})
}
cities.print()
In WebStorm, I expected that console printing out 'true' but it says window is not defined!
In the browser like Chrome, console printed out 'true'.
Isn't window the very antecedent object in js?
How can this happen?
The window object is only defined in browser, and isn't defined in Node.js environment. And it looks like you are running your code with Node.js (by choosing Run from the right-click menu of your .js file).
You need to include your javaScript code in .html file via <script> tag and then right-click this .html file in WebStorm and choose either Run or Debug to get the code executed in browser

Blackberry 10 screenshot error

I am trying to use bb:system::screenshot like so
request.setTarget("sys.pim.uib.email.previewer");
// Set the action that the target app should execute
request.setAction("bb.action.VIEW");
// Set the MIME type of the data
request.setMimeType("message/rfc822");
// Specify the location of the data
request.setUri(QUrl("pim:message/rfc822:" + QString::number(accountId) +":" + QString::number(messageId)));
InvokeTargetReply *reply = invokeManager.invoke(request);
sleep( 2 );
//Screenshot
bb::system::Screenshot screenshot;
QString filename = screenshot.captureDisplay();
if (filename.isEmpty()) {
// Something went wrong
bb::system::ScreenshotError::Type error = screenshot.error();
}
//Close Email
invokeManager.closeChildCard();
The problem is the filename is always empty and screenshot.error() always has error values. Here's what the debugger says at the "something went wrong" line:
screenshot.error() Error: Multiple errors reported.\ Failed to execute MI command: -var-create - * screenshot.error() Error message from debugger back end: Couldn't find method bb::system::screenshot::error\ Unable to create variable object
Not sure what this means?
EDIT: I moved the screenshot variable definition outside the if statement and see this error:
Name : error
Details:bb::system::ScreenshotError::PermissionDenied
Default:bb::system::ScreenshotError::PermissionDenied
Decimal:2
Hex:0x2
Binary:10
Octal:02
Problem is, I definitely have the permissions set correctly:
<!-- Request permission to execute native code. Required for native applications. -->
<permission system="true">run_native</permission>
<permission>access_pimdomain_contacts</permission>
<permission>read_device_identifying_information</permission>
<permission>access_pimdomain_messages</permission>
<permission>bbm_connect</permission>
<permission>access_pimdomain_calendars</permission>
<permission>use_camera</permission>
<permission>use_camera_desktop</permission>
<permission>use_gamepad</permission>
<permission>access_internet</permission>
<permission>access_location_services</permission>
<permission>record_audio</permission>
<permission>read_personally_identifiable_information</permission>
<permission>access_pimdomain_notebooks</permission>
<permission>access_notify_settings_control</permission>
<permission>access_phone</permission>
<permission system="true">_sys_inject_voice</permission>
<permission>read_phonecall_details</permission>
<permission>access_pimdomain_calllogs</permission>
<permission>control_phone</permission>
<permission>post_notification</permission>
<permission system="true">_sys_use_consumer_push</permission>
<permission>run_when_backgrounded</permission>
<permission system="true">_sys_run_headless</permission>
<permission system="true">_sys_headless_nostop</permission>
<permission>access_shared</permission>
<permission>access_sms_mms</permission>
<permission>access_wifi_public</permission>
From the docs, I need "Permissions:
The application must have the use_camera_desktop and access_shared permissions to use this feature."
This is apparently a bug in the Blackberry simulator going as far back as the 10.2 simulator (the 10.2 simulator is when the screenshot library was introduced so you can't go back farther than that.) I confirmed this with a Blackberry Developer Advisor. (found here, sign-in required http://supportforums.blackberry.com/t5/Native-Development/Error-Capturing-and-saving-a-screen-shot-in-BB10-c/td-p/3112490)

Updating performance counters from unmanaged code

I am having problem with previewing custom performance counters with PerflibV2.
Performance Monitor shows my custom performance counter group by GUID, and when I want to expand it "Can't load counters" is shown.
I tried adding myself to "Performance Monitor Users" and "Performance Log Users" groups with no success.
I googled it, and read a lot of MSDN articles, but no success.
Is someone familiar with this problem?
Following is detailed procedure how I created and added custom performance counter:
I need to create a performance counter that will be updated from my unamanged application.
There are two approaches that I found:
Wrapping managed performance counter API, which is not an option because it will impact performance;
Using PerflibV2 which provides needed functionality;
As a test application, I created following schema.xml schema describing custom performance counter:
<!-- <?xml version="1.0" encoding="UTF-16"?> -->
<instrumentationManifest
xmlns="http://schemas.microsoft.com/win/2004/08/events"
xmlns:win="http://manifests.microsoft.com/win/2004/08/windows/events"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<instrumentation>
<counters xmlns="http://schemas.microsoft.com/win/2005/12/counters">
<provider callback = "custom"
applicationIdentity = "PerfCounters.exe"
providerType = "userMode"
providerGuid = "{ab8e1320-965a-4cf9-9c07-fe25378c2a23}">
<counterSet
guid = "{ad36a036-c923-4794-b696-70577630b5cf}"
uri = "Microsoft.Windows.System.PerfCounters.MyCounterSet1"
name = "My LogicalDisk"
description = "This is a sample counter set with multiple instances."
instances = "multiple">
<counter id = "1"
uri = "Microsoft.Windows.System.PerfCounters.MyCounterSet1.MyCounter1"
name = "My Free Megabytes"
description = "First sample counter."
type = "perf_counter_rawcount"
detailLevel = "standard"
defaultScale = "1"/>
</counterSet>
</provider>
</counters>
</instrumentation>
</instrumentationManifest>
And executed:
ctrpp schema.xml
I added created files to my test application, and in my test app, roughly:
PerfAutoInitialize();
ULONG instanceId = 0;
wchar_t instanceName[] = {'t', 'e', 's', 't', 0};
PPERF_COUNTERSET_INSTANCE b = PerfCreateInstance(hDataSource_schema_1, &CtrSetGuid_schema_1_1, instanceName, instanceId);
I installed performance counters with:
lodctr /m:schema.xml
My PerfCounters application is up and running while trying to read counters from Performance Monitor.
There are a few reasons why registering your PerfCounter provider would fail:
Check that the schema of your manifest file is valid. You can validate the file against a XSD definition file provided by Microsoft.
If you'd like to check, register your manifest using the lodctr tool. Make sure your run the lodctr tool as Administrator. If your rights are insufficient, it will fail silently. Once your manifest is registered, you should be able to see the GUID of your CounterSet in the PerfCounter Dialog. (See Browsing Performance Counters for a tool that can list providers.)
You have to generate both the header file and the resource file (use the -rc and -o options of ctrpp). The resource file has to be added to your solution.
Build your application, then re-run the lodctr tool while having both the manifest file and your .exe in the current directory. Ensure that the manifest file points to the filename of your binary in the applicationIdentity property of the provider:
<provider symbol="MyProvider" applicationIdentity="PerfCounterTest.exe" providerName="PerfCounterTest"
Run the app. While the app is running, you should be able to see your provider's name in the browser dialog:
What was exact problem with my sample I am not completely sure but, there is an Microsoft example with Microsoft Windows SDK for Windows 7 and .NET Framework 3.5 SP1:
http://www.microsoft.com/en-us/download/confirmation.aspx?id=3138
After istalling MS SDK sample is located at:
C:\Program Files\Microsoft SDKs\Windows\v7.0\Samples\winbase\PerfCounters\Basic\CPP
It should be adapted for Windows 7 (ctrcpp has fewer arguments, and PerfAutoInitialize() and PerfAutoCleanup() are used instead of CounterInitialize() and CounterCleanup()).
It app crashes on adding counter from perfmon, see:
Perflib 2 crashes when adding a counter (from Perfmon)
I know this question is already answered, but I hit the same issue, and it was caused by me forgetting to include the .RC file generated into executable. When I recompiled the executable including the .RC file with stringtable, unlodctred and lodctred the schema file, it started to work.