I want to use a command-line with a argument to call my cocoa app , but in my cocoa app ,how to receive the argument , this argument is a file path, thank you very much!
Neat thing: use NSUserDefaults.
If you do:
./MyCocoaApp -argument /path/to/file.txt
Then in your code you can do:
NSDictionary * arguments = [[NSUserDefaults standardUserDefaults] volatileDomainForName:NSArgumentDomain];
NSString * path = [arguments objectForKey:#"argument"];
The key is the -argument switch, and the value is the thing that comes after it. Note that this isn't very flexible (you can't do combine options: -a -l ≠-al), but for rudimentary arguments, this is dead simple.
edit with multiple arguments:
./MyCocoaApp -arg1 42 -arg2 "Hello, world!" -arg3 /path/to/file.txt
And then extract via:
... = [arguments objectForKey:#"arg1"];
... = [arguments objectForKey:#"arg2"];
... = [arguments objectForKey:#"arg3"];
The normal main function in Cocoa passes the command line arguments to NSApplicationMain. The arguments are ignored by NSApplicationMain, but you are free to parse them as needed. There are a few standard ways to parse command line arguments, like getopt, or you can just access the values directly.
int main( int argc , char **argv ) {
if ( argc == 2 ) gPathArgument = argv[1];
NSApplicationMain( argc , argv );
}
Note that launch services may pass command line arguments when an application is opened normally, for example when double clicked in the Finder. Be sure to handle unrecognized arguments.
In the special case of a file path to an existing file you can use open like this:
open -a /path/to/your.app /path/to/the/file
And then implement this in your application delegate:
- (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename;
Related
I tried to create a Class and the constructor always gave me a Syntax Error about the *new method then I just copied the Example from the documentation:
MyClass {
// this is a normal constructor method
*new { | arga, argb, argc |
^super.new.init(arga, argb, argc)
}
init { | arga, argb, argc |
// do initiation here
}
}
and still got this:
ERROR: syntax error, unexpected '*', expecting '}'
in interpreted text
line 6 char 5:
*new { | arga, argb, argc |
^
^super.new.init(arga, argb, argc)
-----------------------------------
ERROR: Command line parse failed
-> nil
From my own class i get the same error concerning the constructor. Where am I wrong?
If you check out the Writing Classes helpfile, there's a bit at the top that's easy to miss about where to save your classes.
https://depts.washington.edu/dxscdoc/Help/Guides/WritingClasses.html
NOTE: Class definitions are statically compiled when you launch
SuperCollider or "recompile the library." This means that class
definitions must be saved into a file with the extension .sc, in a
disk location where SuperCollider looks for classes. Saving into the
main class library (SCClassLibrary) is generally not recommended. It's
preferable to use either the user or system extension directories.
Platform.userExtensionDir; // Extensions available only to your user account
Platform.systemExtensionDir; // Extensions available to all users on the machine
It is not possible to enter a class definition into an interpreter
window and execute it.
The the Save As Extension option under the file menu. Then recompile the interpretter and try using your class.
I have a korn shell script which will pass 4 parameters to a Pro*C file
The syntax of the korn shell script are below:
### $command_dir/proc_file_name / $deptid $txdate $pid
### I hardcode the values for testing
$command_dir/proc_file_name / 701 20170109 201701094444001
The syntax of the Pro*C file:
....
main(argc, argv)
int argc
char *argv[];
username.len=strlen(argv[1]);
strncpy((char*)username.arr, argv[1],username.len);
username.arr[username.len]='\0';
deptid.len=strlen(argv[1]);
strncpy((char*)deptid.arr, argv[1],deptid.len);
deptid.arr[deptid.len]='\0';
txdate.len=strlen(argv[1]);
strncpy((char*)txdate.arr, argv[1],txdate.len);
txdate.arr[txdate.len]='\0';
pid=atoi(argv[4]);
printf("\n%s\n", username);
printf("\n%d\n", deptid);
printf("\n%d\n", txdate);
printf("\n%d\n", pid);
....
I found that the values of the parameters were not I put.
Please help...
Many Many thanks
You are using the same array index of 1 for username, deptid, and txdate. Correct that and you would be good.
Accessing argv[1] etc., without checking argc isn't a good practice. When invoked without arguments, your code will result in a core dump.
Also, I don't think your code has the right syntax. Can you please paste the code that compiles?
Suppose I have a file "test.js":
var args = require('yargs')
.command('command', 'command usage here', {alias: "c"} )
.argv;
Then I run:
>node test command
I got this error:
second argument to option must be an object
If I remove the 3rd parameter of .command:
var args = require('yargs')
.command('command', 'command usage here')
.argv;
Everything is fine.
I must make a dumb mistake. But I just cannot figure it out.
Thanks
Your 3rd argument is not required, that's why it works when you remove it. I'm pretty sure the 3rd argument has to be a function call.
var args = require('yargs')
.command('command',
'command explanation',
function(yargs){
//insert yargs.options here
yargs.options({
c:{
demand: true,//if you require the command
alias: 'c',// you will enter -c to use it
description: 'explain what it does'
}
});
})
.argv;
an example of usage might be:
C:\WorkingDirectory>node app.js command -c run
your code could include console.log(args.c);//prints out run
Using the built in macro:
NSLocalizedStringWithDefaultValue(#"fantasy-group.group-rank.stats-label", nil, [NSBundle mainBundle], #"Group Rank", nil);
Results in the following block in the resultant .xliff file when I export for localizations:
<trans-unit id="fantasy-group.group-rank.stats-label">
<source>Group Rank</source>
<note>No comment provided by engineer.</note>
</trans-unit>
This works as expected since source:
The main advantage to these macros is that they can be parsed by the
genstrings tool and used to create your application’s strings files.
But if I try and get snazzy with my own macro to avoid specifying table and bundle, and possibly to specify my own table in an effort to split up the string file:
#define WSLLocalizedString(key, val, comment) \
[[NSBundle mainBundle] localizedStringForKey:(key) value:(val) table:nil]
[WSLLocalizedString(#"fantasy-group.group-rank.stats-label", #"Group Rank", nil);
It doesn't get picked up by XCode or the associated command line tool when I try to generate the xliff file:
$ xcodebuild -exportLocalizations -localizationPath WSL/Translations/ -project WSL.xcodeproj
If I were just doing genstrings, I could do the following source:
find . -name *.m | xargs genstrings -o en.lproj -s WSLLocalizedString
But I want xliffs. Is there a param I can pass to xcodebuild to keep this dream alive?
Not sure about Objective-C, but I have done this for Swift by redefining the NSLocalizedString function like that:
public func NSLocalizedString(key: String, tableName: String? = nil, bundle: NSBundle = NSBundle.mainBundle(), value: String = "", comment: String) -> String
{
return yourBundleHere.localizedStringForKey(key, value: value, table: tableName)
}
In my case I was needed to use custom NSBundle.
I have a bash script that creates a csv file and an R file that creates graphs from that.
At the end of the bash script I call Rscript Graphs.R 10
The response I get is as follows:
Error in is.vector(X) : subscript out of bounds
Calls: print ... <Anonymous> -> lapply -> FUN -> lapply -> is.vector
Execution halted
The first few lines of my Graphs.R are:
#!/bin/Rscript
args <- commandArgs(TRUE)
CorrAns = args[1]
No idea what I am doing wrong? The advice on the net appears to me to say that this should work. Its very hard to make sense of commandArgs
With the following in args.R
print(commandArgs(TRUE)[1])
and the following in args.sh
Rscript args.R 10
I get the following output from bash args.sh
[1] "10"
and no error. If necessary, convert to a numberic type using as.numeric(commandArgs(TRUE)[1]).
Just a guess, perhaps you need to convert CorrAns from character to numeric, since Value section of ?CommandArgs says:
A character vector containing the name
of the executable and the
user-supplied command line arguments.
UPDATE: It could be as easy as:
#!/bin/Rscript
args <- commandArgs(TRUE)
(CorrAns = args[1])
(CorrAns = as.numeric(args[1]))
Reading the docs, it seems you might need to remove the TRUE from the call to commandArgs() as you don't call the script with --args. Either that, or you need to call Rscript Graphs.R --args 10.
Usage
commandArgs(trailingOnly = FALSE)
Arguments
trailingOnly logical. Should only
arguments after --args be returned?
Rscript args.R 10 where 10 is the numeric value we want to pass to the R script.
print(as.numeric(commandArgs(TRUE)[1]) prints out the value which can then be assigned to a variable.