5

I want to programing a function about open the file directly. Like python code:

os.system("ls")

For example, when I use this function (fun_open "/path/to/file"), the system will open the file use the default app. If file is a .txt, open it with textedit.

How to make it?

----UPDATE 9/24/2015-----

My code is:

(defun open_by_system (dir)
  (sb-ext:run-program "/usr/bin/open" (list "-a" "Preview" dir)))

and I use it:

CL-USER> (open_by_system "~/Desktop/ML.pdf")
#<SB-IMPL::PROCESS :EXITED 1>

Nothing else happen

ccQpein
  • 705
  • 7
  • 20
  • What do you mean by "default app"? What OS is this? – melpomene Sep 21 '15 at 20:00
  • 1
    possible duplicate of [SBCL Run Shell Command](http://stackoverflow.com/questions/28956010/sbcl-run-shell-command) – Joshua Taylor Sep 21 '15 at 20:56
  • 3
    @ccQpein I think you're confusing two issues here. One is how to run a shell command (according to other question, use **sb-ext:run-program**). But then the other behavior you're describing (opening the file with a "default" application) is usually provided by another utility on the system. E.g., on some Linux machines, you could run `xdg-open file` to open `file` in the "default" viewer/application. On Windows, I think you can use `explorer file`, and on OS X `open file`. (But I don't know whether any of those are the *best* options, just ones that have worked for me in the past.) – Joshua Taylor Sep 21 '15 at 21:00
  • 1
    @JoshuaTaylor The usual command on windows is `start file`. I'm surprised explorer works. – Random832 Sep 21 '15 at 21:20
  • @Random832 I'd never spent much time looking into it, but yeah, explorer works for a lot of things ([and it looks like I'm not the only one who's come across it](http://superuser.com/a/844405/240381)). It does appear that [start](https://technet.microsoft.com/en-us/library/bb491005.aspx) is the better option, though. – Joshua Taylor Sep 21 '15 at 21:30
  • @JoshuaTaylor you are right, I am not confusing the difference, but my english skill can't express that. Thank you anyway! And now I am searching how to solve the exited code question. – ccQpein Sep 22 '15 at 18:09
  • `"Preview"` is not a useful path. See my example. You might also look at the documentation for `open -a`, to see what it expects. – Rainer Joswig Sep 24 '15 at 19:07

3 Answers3

4

I'd recommend using UIOP, which provides portable interface to the OS and is universally available as a part of ASDF3:

(uiop:run-program "ls")

See the docstrings in run-program.lisp for details.

If you need more convenience functions, you could take a look at inferior-shell.

3

I recommend you to take a look at the available libraries on quickdocs:

link

I recommend you to use inferior-shell available on quicklisp

link

loading:

CL-USER> (ql:quickload 'inferior-shell)
To load "inferior-shell":
  Load 5 ASDF systems:
    alexandria asdf closer-mop named-readtables optima
  Install 4 Quicklisp releases:
    fare-mop fare-quasiquote fare-utils inferior-shell
; Fetching #<URL "http://beta.quicklisp.org/archive/fare-quasiquote/2015-06-08/fare-quasiquote-20150608-git.tgz">
; 15.08KB
==================================================
15,437 bytes in 0.10 seconds (157.03KB/sec)
; Fetching #<URL "http://beta.quicklisp.org/archive/fare-utils/2015-06-08/fare-utils-20150608-git.tgz">
; 31.51KB
==================================================
32,264 bytes in 0.14 seconds (218.80KB/sec)
; Fetching #<URL "http://beta.quicklisp.org/archive/fare-mop/2015-06-08/fare-mop-20150608-git.tgz">
; 2.67KB
==================================================
2,738 bytes in 0.00 seconds (0.00KB/sec)
; Fetching #<URL "http://beta.quicklisp.org/archive/inferior-shell/2015-06-08/inferior-shell-20150608-git.tgz">
; 12.87KB
==================================================
13,182 bytes in 0.00 seconds (12873.05KB/sec)
; Loading "inferior-shell"
[package fare-utils]..............................
[package fare-stateful]...........................
[package fare-quasiquote].........................
[package fare-mop].............
(INFERIOR-SHELL)

a simple sample:

CL-USER> (inferior-shell:run/ss '(echo (1) "2" (+ 3)))
"1 2 3"
NIL
0

a sample with pipes:

CL-USER> (inferior-shell:run/ss `(inferior-shell:pipe (echo (+ hel "lo,") world) (tr "hw" "HW") (sed -e "s/$/!/")))
"Hello, World!"
NIL
0
anquegi
  • 11,125
  • 4
  • 51
  • 67
1

Mac OS X and SBCL:

Open a text file in the default text editor application TextEdit:

Lisp Machine:~ lispm$ touch /tmp/test.text

Lisp Machine:~ lispm$ sbcl
This is SBCL 1.2.14, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.

SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses.  See the CREDITS and COPYING files in the
distribution for more information.

* (sb-ext:run-program "/usr/bin/open" '("/tmp/test.text"))

#<SB-IMPL::PROCESS :EXITED 0>

Open the file with LispWorks as the text editor:

* (sb-ext:run-program
    "/usr/bin/open"
    '("-a"
      "/Applications/LispWorks 7.0 (64-bit)/LispWorks (64-bit).app"
      "/tmp/test.text"))

You might want to consult the SBCL manual for such questions. For example the chapter on Running External Programs.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
  • **sb-ext:run-program** is the answer that came up in the possible duplicate, [SBCL Run Shell Command](http://stackoverflow.com/q/28956010/1281433), too. – Joshua Taylor Sep 21 '15 at 20:57
  • I code the function, but the exit code is 1, and textedit(actually I open pdf it maybe preview.app) don't open. – ccQpein Sep 22 '15 at 18:03
  • @JoshuaTaylor I'm sorry about that! My target is make OS X open file with App installed in system. I use your solution, and compile good, but nothing happen unless give me "". For instance, I want open test.pdf by Preview.app with CL. I'm sorry for my english. – ccQpein Sep 23 '15 at 21:55