0

名前と絶対パスで新しい仮想ホストを追加する AppleScript を作成しようとしています。

テスト ファイル (デスクトップ上の txt ファイル) を試すと機能しますが、実際のファイル (/private/etc/hosts および /private/etc/apache2/extras/httpd-vhosts.conf) を試すと機能しません。

問題はユーザー権限のどこかにあるようですが、管理者として書き込み用にファイルを開く方法がわかりません。「シェルスクリプトを実行」と同じ方法で試しましたが、機能しません (おそらく、間違った構文を使用している可能性があります)。

私のコード:

on run (input)
display dialog "New VirtualHost name" default answer ""
set VirtualHostName to text returned of result

display dialog "Absolute path to VirtualHost's root" default answer "/Library/Webserver/Documents"
set VirtualHostRoot to text returned of result

try
    set hostsNL to "\n127.0.0.1 " & VirtualHostName

    set httpdVhostNL to "\n<VirtualHost 127.0.0.1:80>
ServerAdmin foo@example.com
ServerName " & VirtualHostName & ".local
DocumentRoot '" & VirtualHostRoot & "'
ErrorLog '/private/var/log/apache2/" & VirtualHostName & "-error_log'
CustomLog '/private/var/log/apache2/" & VirtualHostName & "-access_log' common </VirtualHost>"

    set hosts to open for access (POSIX file "/private/etc/hosts") with write permission
    write hostsNL to hosts starting at eof
    close access hosts

    set httpdVhosts to open for access (POSIX file "/private/etc/apache2/extras/httpd-vhosts.conf") with write permission
    write httpdVhostNL to httpdVhosts starting at eof
    close access httpdVhosts


    do shell script "apachectl graceful" with administrator privileges


    return true
on error
    try
        close access hosts
        close access httpdVhosts
    end try
    return false
end try end run

または: 仮想ホストを作成する他の簡単な方法はありますか?

4

1 に答える 1

1

作業の大部分を AppleScript で実行するのではなく、シェル スクリプトで実行します。仮想ホスト名とドキュメント ルートをコマンド ライン引数としてシェル スクリプトに渡します。シェル スクリプトは管理者権限で実行します。

于 2010-12-11T14:38:14.033 に答える