Running AppleScript from XPCOM - applescript

I've been trying to execute applescript code through XPCOM, but no matter what I do it just doesn't seem to work, the observer tells me that the process finishes without a problem, but nothing happens.
var processArgs = ['-e','\'tell application "iTunes"\'','-e','\'activate\'','-e','\'end tell\''];
var file = Cc["#mozilla.org/file/local;1"].createInstance(Components.interfaces.nsIFile);
file.initWithPath( '/usr/bin/osascript');
var process = Cc["#mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
process.init( file );
var observer = {
observe: function( subject, topic ) {
SiteFusion.consoleMessage('DONE ' + topic);
}
}
process.runAsync( processArgs, processArgs.length, observer );
The output in the console is 'DONE process-finished' so it should have worked, does anyone know why this script refuses to open iTunes?

Reason is that you use escaped single quotes in arguments, so your actual resulting AppleScript looks like:
'tell application "iTunes"'
'activate'
'end tell'
Which is clearly not valid. Instead, your arguments should look like this:
const processArgs = ['-e', 'tell application "iTunes"', '-e', 'activate', '-e', 'end tell'];
or, alternatively,
const processArgs = ['-e', `
tell application "iTunes"
activate
end tell`];
or, alternatively,
const processArgs = ['-e', 'tell application "iTunes" to activate'];

Related

JScript: identifying whether double quotes are passed to a WSH script

There are situations when it is important to identify whether double quotes are passed as arguments to a WSH script. For example because they should be passed to another executable to be run.
The standard parsing functions/objects:
objArgs = WScript.Arguments;
for (i = 0; i < objArgs.length; i++)
{
WScript.Echo(objArgs(i));
}
do not differentiate between:
cscript foo.js "bar"
and
cscript foo.js bar
Is it possible with some other approach?
Note: I also tried to sort of escape them with several combinations like:
cscript foo.js '"bar"'
It seems that they are simply stripped away.
Following #Ekkehard.Horner suggestions:
Solution
// parseArgs.js
// Parsing jscript script arguments verbatim
var Shell = new ActiveXObject("WScript.Shell"),
wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\cimv2"),
guid = (new ActiveXObject("Scriptlet.TypeLib")).GUID.substring(0,38),
windir=Shell.ExpandEnvironmentStrings("%WinDir%"),
winver="\"" + windir + "\\System32\\winver.exe\" " + guid,
pcol, pid, cmd;
// Run winver.exe hidden and get this script ID as its ParentProcessId
winver=winver.replace(/\\/g, "\\\\");
Shell.Run("winver " + guid, 0);
pcol = new Enumerator (wmi.ExecQuery(
"SELECT * From Win32_Process WHERE CommandLine='"+ winver + "'",
"WQL", 32));
for (; !pcol.atEnd(); pcol.moveNext()){
var prc = pcol.item();
pid=prc.ParentProcessId;
prc.Terminate;
}
// Get the command line for the found PID
pcol = new Enumerator (wmi.ExecQuery(
"SELECT * From Win32_Process WHERE ProcessID="+ pid,
"WQL", 32));
for (; !pcol.atEnd(); pcol.moveNext()){
var prc = pcol.item();
cmd =prc.CommandLine;
}
WScript.Echo(cmd);
// Parse command line for arguments
var ags,
parseCmd=function(cmd){// WMI trims initial spaces
var p = new Object(),
re =/^"/.test(cmd) ? /"[^"]+" */ : /\S+\s*/;
p.nxt=re.test(cmd) ? cmd.match(re)[0] : ""; // extract next token
p.rst=cmd.replace(re, "") ; // remainder
return(p);
}
// Strip c/wscript path
ags=parseCmd(cmd).rst
//WScript.Echo(ags);
// Remove WSH "//xxx" options
ags=ags.replace(/\/\/\w+ +/g, "")
//WScript.Echo(ags);
// Strip script name and get arguments
ags=parseCmd(ags).rst
WScript.Echo(ags);
// Loop args and store as an array
var i=1, aags=[];
while(ags != ""){
var p =parseCmd(ags);
ags=p.rst;
aags.push(p.nxt.replace(/ +$/, ""));
WScript.Echo(i, p.nxt);
i++;
}
WScript.Echo(aags);
Test
Running parseArgs.js gives:
> cscript //nologo parseArgs.js "hello" world
cscript //nologo parseArgs.js "hello" world
"hello" world
1 "hello"
2 world
"hello",world
The line:
> parseArgs.js "hello" world
gives similar results.
Comments
Do we need such a convoluted script? Short answer: no. Long: depends.
In general, assuming you know the name of your script when it is run, you could query WMI for it.
Anyway, when you deploy your script, you do not normally have control on the deploy directory. So, if there is another script running under the same name, you can't know for sure which one is yours.
Another not so edge case is when there are two or more instances of your script running.
The strategy here is to run some dummy standard Windows executable (winver.exe) hidden, passing to it a GUID. In this way, it is safe to identify winver.exe command line by the unique GUID and consequently your script as the parent of winver.exe.
winver.exe does not require arguments, but does not protest if you pass some to it.

How to translate this slider value change from AppleScript to JavaScript

This bit of AppleScript works. If I have the System Preferences Sound panel open, and run it in the Script Editor app, it changes the volume to be at 50%.
tell application "System Events"
tell process "System Preferences"
set v to value of slider 0 of window 0
log v
set value of slider 0 of window 0 to 0.5
end tell
end tell
This, which tries to be the same thing, fails. Anyone know how to fix it?
var se = Application("System Events");
var spp = se.processes["System Preferences"];
spp.windows[0].sliders[0].value = 0.5
var curr = spp.windows[0].sliders[0].value();
console.log("Current value: " + curr + " - " + typeof(curr));
It ends up setting it to 0. It seems I can only set the volume to 0 or 1. In reality I'm trying to script another application, but this boils down the problem.
As I noted in my comment, I'm 90% sure this is a bug.
Here's a workaround:
app = Application.currentApplication();
app.includeStandardAdditions = true;
try {
app.doShellScript('osascript -e \'tell application "System Events" to set value of slider 0 of window 0 of process "System Preferences" to 0.2\'');
} catch (error ) {
-1;
}

print pdf directly to printer windows

I have a windows app that prints pdfs directly to a printer. Everything is working but for some reason for each pdf to print I see the pdf reader Nitro Pro pop up in the background then close.
Is there a way to keep the window from poping up. It does not seem to effect the over application but just kind of annoying.
private void PrintDocument(string printer, string fileName)
{
ProcessStartInfo info = new ProcessStartInfo
{
Arguments = "\"" + printer + "\"",
Verb = "PrintTo",
FileName = fileName,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = true
};
Process p = new Process { StartInfo = info };
p.Start();
p.WaitForExit(5000);
if (p.HasExited == false)
{
p.Kill();
}
}
This is not possible.
Windows can't print a file directly, it relies on a program to do so. It will use whatever application has configured itself to handle the PrintTo verb for the particular file extension. In your case it appears the application is Nitro Pro.
It's possible that you can find and install an application that can print the file without opening a window to do so, but that's beyond the scope of StackOverflow.

D: executeShell on Windows to run another program not returning immediately

I'm using D as a scripting language for Windows 7 console stuff to automate boring tasks. One of my scripts (open.exe) is supposed to allow me to open stuff from the command line without me having to specify which program I use (I have a configuration file with this stuff). Now, I use executeShell to do this, and call something like start [name of program I want to use] [name of input file]. If I do this directly from the shell, it returns immediately, but if I do it using my D script, it doesn't return until the program that it opens is closed. What should I do to allow it to return immediately?
For reference purposes, this is the business logic of my script (the main method just does some argument parsing for piping purposes):
immutable path = "some//path//going//to//config//file.conf";
void process(string input) {
string extension = split(input,".")[1]; //get file extension from input
auto config = File(path,"r"); auto found = false;
while (!config.eof()){
auto line = chomp(config.readln());
if (line[0]!='#') { //skip comment lines
auto divided = split(line, ":");
if (divided[0] == extension) {
found = true;
auto command = "start " ~ divided[1] ~ " " ~ input;
auto result = executeShell(command);
//test for error code and output if necessary
writeln(result.output);
}
}
}
if (!found)
writeln("ERROR: Don't know how to open " ~ input);
}
From the top of the std.process documentation:
Execute and wait for completion, collect output - executeShell
The Windows start program spawns a process and exits immediately. D's executeShell does something else. If you'd like to spawn another program, use the appropriate functions: spawnProcess or spawnShell.

Cocoa - Force User To Switch?

I am building a mac application, that at a certain time, needs to switch the currently logged in user to a different, preset one. Essentially a modified login window.
Is there a way to do this using cocoa?
-- Ari
Edit: Is there a way to not require the user to input their password?
Before I say my solution, I want to say that #jrodatus's answer is excellent, it just is for a slightly different use case.
I came up with this little applescript:
set theUser to "user"
set theUser to do shell script "/usr/bin/id -u " & theUser
set password to "pswd"
do shell script "/System/Library/CoreServices/Menu\\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID " & theUser
repeat
try
tell application "System Events"
repeat until visible of process "SecurityAgent" is false
set visible of process "SecurityAgent" to false
end repeat
keystroke password
keystroke return
end tell
exit repeat
on error
tell application "System Events"
end tell
end try
end repeat
This simply triggers the login screen, with -switchToUserID set to the username's user id. Then when at least one window of SecurityAgent (The login screen) is visible, simulate the keystroke of the password, then enter the result is when run the login window opens, with the password typed in. Also, this has no delays.
As explained in answer to a similar question here, there is a command-line tool called "CGSession" hidden in the System folder that should do what you need. To run a command-line tool inside a Cocoa application, look into NSTask.
To switch users directly, find out the unix user ID of your preset user by running "id -u theUserName" and then use the output as the argument for executing:
/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID theUserIDNumber
Or to simply get to the login window (without logging out), run:
/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend
Here is a quick Obj-C category for NSWorkspace.
NSWorkspace-SwitchUser.h:
#import <Cocoa/Cocoa.h>
#interface NSWorkspace (SwitchUser)
-(BOOL)switchToUser:(NSString *)userName;
#end
NSWorkspace-SwitchUser.m:
#import "NSWorkspace-SwitchUser.h"
#import <sys/types.h>
#import <pwd.h>
#import <stdlib.h>
#import <unistd.h>
#import <stdio.h>
#implementation NSWorkspace (SwitchUser)
-(BOOL)switchToUser:(NSString *)userName {
struct passwd *pwd = malloc(sizeof(struct passwd));
if (!pwd) {
NSLog(#"Couldn't allocate struct passwd for getpwnam_r.");
return FALSE;
}
size_t buf_len = sysconf(_SC_GETPW_R_SIZE_MAX) * sizeof(char);
char *buffer = malloc(buf_len);
if (!buffer) {
NSLog(#"Couldn't allocate buffer for getpwnam_r.");
return FALSE;
}
getpwnam_r([userName UTF8String], pwd, buffer, buf_len, &pwd);
if (!pwd) {
NSLog(#"getpwnam_r failed to find the requested user.");
return FALSE;
}
uid_t userID = pwd->pw_uid;
free(pwd);
free(buffer);
// Run CGSession with the -switchToUserID argument
NSTask *cgsTask = [NSTask launchedTaskWithLaunchPath:#"/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession"
arguments:[NSArray arrayWithObjects:#"-switchToUserID",[NSString stringWithFormat:#"%u",userID],nil]];
// Wait till the task completes.
// Should be able to use -[NSTask waitUntilExit] instead, but it wasn't working for me :-P
while ([cgsTask isRunning]) {
usleep(100000);
}
return ([cgsTask terminationStatus] == 0);
}
#end
Edit: If you need to switch users without requiring the user to enter their password, there doesn't seem to be any way to do that without AppleScript, which IMO is unsafe in every sense of the word. But you might glean what you need here and here.

Resources