Applescript + Xcode“applicationShouldTerminateAfterLastWindowClosed_(sender)”不工作

时间:2016-07-05 01:54:45

标签: xcode applescript

我创建了一个小脚本来控制httpd / php / mysql,并使用XCode创建并链接到该脚本的接口。一切正常。问题是我试图在单击红色圆圈按钮后关闭应用程序窗口,并且没有发生。应用程序继续运行,只关闭窗口。

我尝试过使用:

applicationShouldTerminateAfterLastWindowClosed_(sender) 

整个脚本是这样的:

script AppDelegate

#### PROPERTY LIST ####
    property parent : class "NSObject"
    property startApache : missing value
    property restartApache : missing value
    property stopApache : missing value
    property editConfig : missing value
    property editVHosts : missing value
    property openDir : missing value
    property resetConfig : missing value
    property editPHP : missing value
    property resetPHPConfig : missing value
    property startMYSQL : missing value
    property stopMYSQL : missing value
    property restartMYSQL : missing value
    property openDirMYSQL : missing value
    property resetMYSQL : missing value

#### APACHE CMDs ####
    on startApache_(sender)
        do shell script "/usr/sbin/apachectl start" with administrator privileges
    end startApache_

    on restartApache_(sender)
        do shell script "/usr/sbin/apachectl restart" with administrator privileges
    end restartApache_

    on stopApache_(sender)
        do shell script "/usr/sbin/apachectl stop" with administrator privileges
    end stopApache_

    on editConfig_(sender)
        do shell script "open -a /Applications/BBEdit.app /private/etc/apache2/httpd.conf"
    end editConfig_

    on editVHosts_(sender)
        do shell script "open -a /Applications/BBEdit.app /private/etc/apache2/extra/httpd-vhosts.conf"
    end editVHosts_

    on openDir_(sender)
        do shell script "open /Library/WebServer/Documents/"
    end openDir_

    on resetConfig_(sender)
        display dialog "Are you sure you want to reset the httpd.conf to it's default settings?\n\n This cannot be undone!" with icon stop with title "Reset Configuration File"
        do shell script "/usr/sbin/apachectl stop" with administrator privileges
        do shell script "cp /private/etc/apache2/httpd.conf.pre-update /private/etc/apache2/httpd.conf ; cp /private/etc/apache2/extra/httpd-vhosts.conf.default /private/etc/apache2/extra/httpd-vhosts.conf" with administrator privileges
    end resetConfig_

#### PHP CMDs ####
    on editPHP_(sender)
        do shell script "open -a /Applications/BBEdit.app  /etc/php.ini"
    end editPHP_

    on resetPHPConfig_(sender)
        display dialog "Are you sure you want to reset the php.ini to it's default settings?\n\n This cannot be undone!" with icon stop with title "Reset Configuration File"
        do shell script "cp /etc/php.ini.default /etc/php.ini" with administrator privileges
    end resetPHPConfig_

#### MYSQL CMDs ####
    on startMYSQL_(sender)
        do shell script "/usr/local/bin/mysql.server start" with administrator privileges
    end startMYSQL_

    on restartMYSQL_(sender)
        do shell script "/usr/local/bin/mysql.server restart" with administrator privileges
    end restartMYSQL_

    on stopMYSQL_(sender)
        do shell script "/usr/local/bin/mysql.server stop" with administrator privileges
    end stopMYSQL_

    on openDirMYSQL_(sender)
        do shell script "open /usr/local/var/mysql/"
    end openDirMYSQL_

    on resetMYSQL_(sender)
        display dialog "Are you sure you want to reset ALL MySQL databases to their default?\n\n This cannot be undone!" with icon stop with title "Reset All Databases"
        display dialog "Type the new root password" default answer "" with hidden answer
        set root_pass to text returned of result
        if root_pass = "" then
            display dialog "Root password cannot be blank!" buttons {"Cancel"} with icon Caution
            error number -128
        else
            display dialog "MySQL DB will be recriated in the background.\n\nClick on reset and wait." buttons {"Cancel","Reset"}
            do shell script "/usr/local/bin/mysql.server stop" with administrator privileges
            do shell script "rm -rf /usr/local/var/mysql" with administrator privileges
            do shell script "/usr/local/bin/mysqld  --initialize-insecure"
            do shell script "/usr/local/bin/mysql.server start"
            do shell script "/usr/local/bin/mysqladmin -u root password '"&root_pass&"'"
            display dialog "All done!\n\nDatabase reseted." buttons {"Ok"} with title "MySQL Reset Utility"
        end if
    end resetMYSQL_

    on applicationWillFinishLaunching_(aNotification)
    end applicationWillFinishLaunching_

    on applicationShouldTerminateAfterLastWindowClosed_(sender)
        return true
    end applicationShouldTerminateAfterLastWindowClosed_

    on applicationShouldTerminate_(sender)
        return current application's NSTerminateNow
    end applicationShouldTerminate_

end script

1 个答案:

答案 0 :(得分:0)

正如@vadian所说:看看文件所有者的代理插座是否正确连接到App Delegate(您的脚本):

File's Owner Outlet to App Delegate

BTW这是创建新的CocoaApplescript-App后的默认设置。我刚刚复制了处理程序

    applicationNo.setCellValueFactory(
            new Callback<TableColumn.CellDataFeatures<GroupRow, ApplicationModel>, ObservableValue<ApplicationModel>>() {
//GroupRow is the object that contains the Application object
                @Override
                public ObservableValue<ApplicationModel> call(CellDataFeatures<GroupRow, ApplicationModel> param) {
                    return param.getValue().getApplicationProperty();
                }
            });
    applicationNo.setCellFactory(
            new Callback<TableColumn<GroupRow, ApplicationModel>, TableCell<GroupRow, ApplicationModel>>() {

                @Override
                public TableCell<GroupRow, ApplicationModel> call(TableColumn<GroupRow, ApplicationModel> param) {
                    return new ApplicationTableCellEditor();
//ApplicationTableCellEditor is custom TableCell
                }
            });
    applicationNo.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<GroupRow, ApplicationModel>>() {

        @Override
        public void handle(CellEditEvent<GroupRow, ApplicationModel> event) {
            event.getRowValue().setApplication(event.getNewValue());
            tableChanged();
            applicationChanged(event.getNewValue());
        }
    });

进入脚本,一切正常!

玩得开心,迈克尔/汉堡