0

我有一个问题,如果需要,我需要它提示输入管理员凭据并且找不到可行的解决方案。

set foo to computer name of (system info)
        set p to (path to desktop)
        set targetFile to "Macintosh HD:private:var:log:system.log:"
        set targetPath to p & "LOGS-I-NEED-" & foo as text

        try
            if ("80" is not in (do shell script "id -G")) then
                tell application "Finder" to duplicate file targetFile to targetPath
                else
                tell application "Finder" to duplicate file targetFile to targetPath
            end if
            on error the error_message number the error_number
            display dialog "Error: " & the error_number & ". " & the error_message & return & "targetFile:" & targetFile & return & return & "targetPath:" & targetPath buttons {"OK"} default button 1

        end try

我也试过这个,但不知道如何 cp 到一个名称中有变量的文件夹。

try
    set foo to computer name of (system info)
    do shell script "sudo cp /Private/var/log/system.log ~/Desktop/{name:LOGS-I-NEED} & foo" with administrator privileges
end try

无论哪种方式都对我有用。

多谢你们!

4

1 回答 1

0

quoted form of用单引号包围字符串并''\''.

do shell script "d=~/Desktop/LOGS-I-NEED-" & quoted form of (computer name of (system info)) & "
mkdir -p \"$d\"
cp /var/log/system.log \"$d\"" with administrator privileges

您还可以使用 systemsetup 获取计算机名称:

do shell script "d=~/\"Desktop/LOGS-I-NEED-$(systemsetup -getcomputername | sed 's/[^:]*: //')\"
mkdir -p \"$d\"
cp /var/log/system.log \"$d\"" with administrator privileges

如果重复的命令需要 root 权限,Finder 应该会显示一个密码对话框。但是您也可以使用以下内容运行这样的脚本sudo osascript Untitled.scpt

set d to "LOGS-I-NEED-" & computer name of (system info) as text
tell application "Finder"
    if not (exists folder d of desktop) then
        make new folder at desktop with properties {name:d}
    end if
    duplicate POSIX file "/var/log/system.log" to folder d of desktop
end tell
于 2013-05-08T13:21:08.540 回答