Re: Open a new terminal tab or window from a Cocoa app at a certain directory without using NSAppleScript?

2012-01-28 Thread Ron Hunsinger

On Jan 26, 2012, at 11:30 AM, Jens Alfke wrote:

 To work around this I suggest using single-quotes instead, and preprocessing 
 the string to insert a backslash in front of any exclamation point or 
 single-quote. I *think* that will be enough.

After a single quote, the *only* character that has a special meaning is 
another single quote, which ends the quotation. Double quotes, back slashes, 
dollar signs, exclamation marks, et al. have absolutely no special significance 
between single quotes..

Thus, the simplest way to safely quote an arbitrary string is to preprocess the 
string to replace each single quote (') with the sequence single quote, 
backslash, single quote, single quote ('\''), and then wrap the resulting 
string in single quotes.

As an (unnecessary) refinement, scan that sequence again, replacing each 
sequence of three consecutive single quotes with one, to shorten the string 
that results when the original string contained consecutive single quotes. As 
another (unnecessary) refinement, pre-scan the original string to see if it 
contains only safe characters, in which case no quoting needs to be done at 
all. It's probably not worth bothering to do either of these.


Almost as easy to program, but generally producing longer quoted strings, is to 
prepend a backslash to any character that is not a lowercase letter or a digit. 
Do not wrap the resulting string in any kind of quotes. (Non-ASCII characters 
are never special to the shell, but I'm not sure what would happen if you 
inserted backslashes in the middle of a multi-byte character, nor whether the 
behavior will change in later versions of the shell, so be sure you're 
prepending backslashes to characters, not bytes.)

-Ron Hunsinger


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Open a new terminal tab or window from a Cocoa app at a certain directory without using NSAppleScript?

2012-01-28 Thread Jens Alfke

On Jan 28, 2012, at 1:17 AM, Ron Hunsinger wrote:

 After a single quote, the *only* character that has a special meaning is 
 another single quote, which ends the quotation.

…or a newline :)

[And yes, I have encountered filenames with newlines in them. It happened after 
I downloaded a PDF with a non-mnemonic filename, then opened it in Preview, 
copied the title of the paper, and pasted it into the filename in the Finder. 
Unfortunately the line break in the title got pasted in too. I found out later 
because several apps, including Dropbox, were very unhappy with the resulting 
file.]

—Jens



smime.p7s
Description: S/MIME cryptographic signature
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Open a new terminal tab or window from a Cocoa app at a certain directory without using NSAppleScript?

2012-01-26 Thread Andrew
I would like to perform the same logic as the New Terminal Tab at Folder
service in Finder in my Cocoa app. The only code I found via Google is all
using AppleScript to open the Terminal.app, but nothing I found was a Cocoa
interface besides 3rd party terminal apps (iTerm, iTerm2). Is there a
better way than using NSAppleScript to do this?

I do not see any core libraries for the terminal app, but perhaps I am just
looking in the wrong location.

If I were to use NSWorkspace, I tried using the open at the command line,
but I am not able to:

   1. figure out how to open a new tab as opposed to a window
   2. not have to run a program
   3. it does not open the default terminal theme

Thanks,
Andrew
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Open a new terminal tab or window from a Cocoa app at a certain directory without using NSAppleScript?

2012-01-26 Thread Andrew
Well, I found this:
http://code.google.com/p/cdto/source/browse/plugins/terminal/CD2Terminal.m?spec=svn20c4d028f197a6810230ddff969de81c4b23876dr=20c4d028f197a6810230ddff969de81c4b23876d

And got the terminal opening in a new window at the path. So I now need to
find how I can set the settings and have it open a new tab instead of
window.

On Thu, Jan 26, 2012 at 10:13 AM, Andrew andrewrwr+cocoa...@gmail.comwrote:

 I would like to perform the same logic as the New Terminal Tab at Folder
 service in Finder in my Cocoa app. The only code I found via Google is all
 using AppleScript to open the Terminal.app, but nothing I found was a Cocoa
 interface besides 3rd party terminal apps (iTerm, iTerm2). Is there a
 better way than using NSAppleScript to do this?

 I do not see any core libraries for the terminal app, but perhaps I am
 just looking in the wrong location.

 If I were to use NSWorkspace, I tried using the open at the command
 line, but I am not able to:

1. figure out how to open a new tab as opposed to a window
2. not have to run a program
3. it does not open the default terminal theme

 Thanks,
 Andrew

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Open a new terminal tab or window from a Cocoa app at a certain directory without using NSAppleScript?

2012-01-26 Thread Andrew
Well, I figured it out for myself. Posting here in case anyone else
wants to do the same:

- (void)openTerminal:(id)sender
{
  TerminalApplication* termApp = [SBApplication
applicationWithBundleIdentifier:@com.apple.terminal];
  NSString *dir = // Get your directory here
  NSString *cmd = [NSString stringWithFormat:@cd \%@\; clear,
dir]; // Assumes bash, which is okay for me, but maybe not others.

  TerminalWindow *window = nil;
  if (termApp.windows.count  0)
  {
// Use the first window:
window = [termApp.windows objectAtIndex:0];
  }

  TerminalSettingsSet *settings = [termApp startupSettings];
  TerminalTab *newTab = [termApp doScript:cmd in:window];
  [newTab setCurrentSettings:settings];
  [newTab setSelected:YES];

  [termApp activate];
}

On Thu, Jan 26, 2012 at 11:02 AM, Andrew andrewrwr+cocoa...@gmail.com wrote:

 Well, I found this:
 http://code.google.com/p/cdto/source/browse/plugins/terminal/CD2Terminal.m?spec=svn20c4d028f197a6810230ddff969de81c4b23876dr=20c4d028f197a6810230ddff969de81c4b23876d

 And got the terminal opening in a new window at the path. So I now need to 
 find how I can set the settings and have it open a new tab instead of window.


 On Thu, Jan 26, 2012 at 10:13 AM, Andrew andrewrwr+cocoa...@gmail.com wrote:

 I would like to perform the same logic as the New Terminal Tab at Folder 
 service in Finder in my Cocoa app. The only code I found via Google is all 
 using AppleScript to open the Terminal.app, but nothing I found was a Cocoa 
 interface besides 3rd party terminal apps (iTerm, iTerm2). Is there a better 
 way than using NSAppleScript to do this?

 I do not see any core libraries for the terminal app, but perhaps I am just 
 looking in the wrong location.

 If I were to use NSWorkspace, I tried using the open at the command line, 
 but I am not able to:

 figure out how to open a new tab as opposed to a window
 not have to run a program
 it does not open the default terminal theme

 Thanks,
 Andrew



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Open a new terminal tab or window from a Cocoa app at a certain directory without using NSAppleScript?

2012-01-26 Thread Jens Alfke

On Jan 26, 2012, at 9:13 AM, Andrew wrote:

 The only code I found via Google is all
 using AppleScript to open the Terminal.app, but nothing I found was a Cocoa
 interface besides 3rd party terminal apps (iTerm, iTerm2). Is there a
 better way than using NSAppleScript to do this?

Not that I’m aware of. NSAppleScript is a perfectly valid way to do 
inter-application communication, so don’t think of this as a kludge.

—Jens

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Open a new terminal tab or window from a Cocoa app at a certain directory without using NSAppleScript?

2012-01-26 Thread Jens Alfke

On Jan 26, 2012, at 10:22 AM, Andrew wrote:

  NSString *cmd = [NSString stringWithFormat:@cd \%@\; clear”, dir]; // 
 Assumes bash, which is okay for me, but maybe not others.

Watch out — that line has quoting problems. If the path to the directory 
contains double-quotes, dollar signs, backslashes, exclamation points or 
various other shell meta-characters, that command will at best fail and at 
worst do something unexpected. There’s even a remote chance this could be used 
as an exploit to run malicious code, depending on how that directory path got 
created. For example, if someone could create a directory named
;rm -rf ~;
(including the quotes) it would be quite dangerous to use your code to open a 
Terminal window on it or any subdirectory of it. Given that many document 
formats are actually packaged directory trees, I can think of ways this could 
be done without a user's knowledge...

To work around this I suggest using single-quotes instead, and preprocessing 
the string to insert a backslash in front of any exclamation point or 
single-quote. I *think* that will be enough.

—Jens
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Re: Open a new terminal tab or window from a Cocoa app at a certain directory without using NSAppleScript?

2012-01-26 Thread Jean-Daniel Dupas

Le 26 janv. 2012 à 20:30, Jens Alfke a écrit :

 
 On Jan 26, 2012, at 10:22 AM, Andrew wrote:
 
 NSString *cmd = [NSString stringWithFormat:@cd \%@\; clear”, dir]; // 
 Assumes bash, which is okay for me, but maybe not others.
 
 Watch out — that line has quoting problems. If the path to the directory 
 contains double-quotes, dollar signs, backslashes, exclamation points or 
 various other shell meta-characters, that command will at best fail and at 
 worst do something unexpected. There’s even a remote chance this could be 
 used as an exploit to run malicious code, depending on how that directory 
 path got created. For example, if someone could create a directory named
   ;rm -rf ~;
 (including the quotes) it would be quite dangerous to use your code to open a 
 Terminal window on it or any subdirectory of it. Given that many document 
 formats are actually packaged directory trees, I can think of ways this could 
 be done without a user's knowledge...
 
 To work around this I suggest using single-quotes instead, and preprocessing 
 the string to insert a backslash in front of any exclamation point or 
 single-quote. I *think* that will be enough.

And before backslash too I think (what if the directory name isd\';rm -rf 
~;\' )

-- Jean-Daniel





___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com