copen——适用于Mac和Windows的打开工具

copen——适用于Mac和Windows的打开工具

这个命令是从outreg2包中的shellout命令修改而来。shellout命令只能用于Windows系统,因此为了方便自己使用,我对其进行了稍微的改写使之也适用与Mac系统。

运行ssc install outreg2命令即可按照shellout命令,打开shellout.ado即可看到它的源代码:

Stata
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
program define shellout
version 7.0
syntax [anything] [using/] [,cd]
if "`c(os)'"=="Windows" | "$S_MACH"=="PC" {
if "`using'"~="" {
winexec cmd /c start "" "`using'"
}
else {
if "`cd'"~="cd" {
cap winexec `anything'
if _rc==193 {
winexec cmd /c start "" "`anything'"
}
if _rc==601 {
noi di in yel "Cannot find `anything'. Make sure typed the name correctly."
}
}
else {
winexec cmd /c cd `c(pwd)'\ & `anything'
}
}
}
else {
local Version7 ""
cap local Version7 `c(stata_version)'

if "`Version7'"=="" {
* stata 7
}
else {
* non-PC systems
di "{opt shellout} probably will not work with `c(os)'"
shell `using'
}
}
end

很容易发现,在这个程序里面进行了操作系统的判断,针对于非Windows系统的电脑会显示运行else部分,即无法使用,所以我们只需要改造一下else部分即可。Mac的shell命令——open有类似的功能,所以就直接放进去就好了,即:

Stata
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
*! 打开指定文件、网址、文件夹
*! 程振兴 2018年7月13日
cap prog drop copen
prog define copen
version 7.0
syntax [anything] [using/] [,cd]
if "`c(os)'"=="Windows" | "$S_MACH"=="PC" {
if "`using'"~="" {
winexec cmd /c start "" "`using'"
}
else {
if "`cd'"~="cd" {
cap winexec `anything'
if _rc==193 {
winexec cmd /c start "" "`anything'"
}
if _rc==601 {
noi di in yel "找不到`anything'."
}
}
else {
winexec cmd /c cd `c(pwd)'\ & `anything'
}
}
}
else {
!open "`anything'"
}
end

为了区别原来的shellout命令,我将这个命令为copen

# Stata

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×