AppleScript 创建 VirtualHost
我正在尝试编写一个 AppleScript,它将按名称和绝对路径添加新的虚拟主机。
当我尝试测试文件(桌面上的txt文件)时它可以工作,但是当我尝试真实文件(/private/etc/hosts和/private/etc/apache2/extras/httpd-vhosts.conf)时它就不起作用。
问题似乎出在用户权限附近,但我不知道如何以管理员身份打开文件进行写入。我尝试了“do shell script”的方法,但它不起作用(可能我使用了不正确的语法)
我的代码:
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 [email protected]
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
或者:还有其他简单的方法来创建虚拟主机吗?
I'm trying to write an AppleScript which will add new virtualhost by name and absolute path.
It works when I try on test files (txt files on desktop) but when I try real files (/private/etc/hosts and /private/etc/apache2/extras/httpd-vhosts.conf) it just does not work.
The problem seems to be somewhere around user permissions, but I have no idea how to open file for writing as administrator. I tried just the way I'm doing 'do shell script' but it does not work (ar probably I'm using incorrect syntax)
My code:
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 [email protected]
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
Or: is there other simlpe way to create virtual hosts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要在 AppleScript 中执行大部分工作,而是在 shell 脚本中执行。传递虚拟主机名&将文档根目录作为 shell 脚本的命令行参数,您可以使用管理员权限执行该脚本。
Rather than performing the bulk of the work in the AppleScript, do it in a shell script. Pass the virtual host name & document root as command line arguments to the shell script, which you execute with admin privileges.