Recent

Author Topic: Open Terminal in a folder (MacOS)  (Read 718 times)

Grahame Grieve

  • Sr. Member
  • ****
  • Posts: 365
Open Terminal in a folder (MacOS)
« on: October 31, 2022, 12:14:29 pm »
I'd like to start a new terminal in a particular folder when the user clicks on the appropriate button. What's the recommended way to start at terminal at a specific location on OSX

My windows code:
proc := TProcess.create(nil);
    try
      proc.CurrentDirectory := FilePath([FManager.FCurrent.FFolder]);
      proc.CommandLine := 'cmd';
      for i := 1 to GetEnvironmentVariableCount do
        begin
        s := GetEnvironmentString(i);
        proc.Environment.Add(s+'=' + GetEnvironmentVariable(s));
      end;
      proc.Execute;
    finally
      proc.free;
    end;     

Zvoni

  • Hero Member
  • *****
  • Posts: 2330
Re: Open Terminal in a folder (MacOS)
« Reply #1 on: October 31, 2022, 12:32:31 pm »
Since MacOS is based on *nix: Isn't the "open"-command also working for Folders? Especially since on *nix Folders are files, too... (or even on Windows if memory serves...)
btw: CommandLine is deprecated
Aircode
Code: Pascal  [Select][+][-]
  1. proc.Executable:='/bin/open';
  2. proc.Parameters.Add(MyPathToFolder);
  3. proc.Execute;
  4.  
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Grahame Grieve

  • Sr. Member
  • ****
  • Posts: 365
Re: Open Terminal in a folder (MacOS)
« Reply #2 on: October 31, 2022, 01:31:48 pm »
executable not found: "/bin/open"

Grahame Grieve

  • Sr. Member
  • ****
  • Posts: 365
Re: Open Terminal in a folder (MacOS)
« Reply #3 on: October 31, 2022, 01:36:20 pm »
/bin/zsh doesn't cause an error, but no terminal appears

Zvoni

  • Hero Member
  • *****
  • Posts: 2330
Re: Open Terminal in a folder (MacOS)
« Reply #4 on: October 31, 2022, 01:38:23 pm »
executable not found: "/bin/open"

https://wiki.freepascal.org/TProcess
Quote
Presumably on Linux/Unix: you must specify the full path to the executable. For example '/bin/cp' instead of 'cp'. If the program is in the standard PATH then you can use the function FindDefaultExecutablePath from the FileUtil unit of the LCL.
https://lazarus-ccr.sourceforge.io/docs/lazutils/fileutil/finddefaultexecutablepath.html

Execute in a Terminal "which open"
Should return the Path the "open"-command is located if it's in the PATH-variable

i used "/bin/open" just as an example
« Last Edit: October 31, 2022, 01:40:19 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2023
  • Former Delphi 1-7, 10.2 user
Re: Open Terminal in a folder (macOS)
« Reply #5 on: October 31, 2022, 02:56:20 pm »
The command to open a Terminal would be:

 /usr/bin/open -n /System/Applications/Utilities/Terminal.app

I have no idea how you would open the Terminal in a specific folder. Prepending the above with the usual UNIX "cd folder_name && " does not work. The Terminal is going to open in the user's home directory regardless of the starting directory.

There is a way to do it using the GUI, right-click on a folder and you have the option on the popup menu to "New Terminal at Folder".

Zvoni

  • Hero Member
  • *****
  • Posts: 2330
Re: Open Terminal in a folder (macOS)
« Reply #6 on: October 31, 2022, 03:06:50 pm »
The command to open a Terminal would be:

 /usr/bin/open -n /System/Applications/Utilities/Terminal.app

I have no idea how you would open the Terminal in a specific folder. Prepending the above with the usual UNIX "cd folder_name && " does not work. The Terminal is going to open in the user's home directory regardless of the starting directory.

There is a way to do it using the GUI, right-click on a folder and you have the option on the popup menu to "New Terminal at Folder".
Huh?
https://superuser.com/questions/579796/what-arguments-does-the-macs-terminal-app-accept
Quote
I've found that running open -a Terminal /path/to/folder will open a new Terminal window at the given folder.
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Grahame Grieve

  • Sr. Member
  • ****
  • Posts: 365
Re: Open Terminal in a folder (MacOS)
« Reply #7 on: October 31, 2022, 09:28:56 pm »
open -a Terminal /path/to/folder works very nicely, thanks

 

TinyPortal © 2005-2018