需要AppleScript的AppleScriptObjC转换帮助


0

我需要在声明中说我不是程序员,所以我需要谈谈。我已经使用指南和大量的试验和错误完成了我在下面描述的内容

我有一个正常运行的AppleScript,它有2个用户名和密码提示,然后根据LDAP查找继续安装一些网络卷。我创建了一个新的AppleScriptObjC Xcode 6.4项目,并从工作的AppleScript中引入了代码。使用界面构建器,我创建了一个简单的输入窗口并将其与以下代码链接起来:

-- IBOutlets
property theWindow : missing value
property accountName : missing value
property passwordName : missing value

-- IBActions (button clicks)
on setCredentialsFromUserInput_(sender)
    set accountName to accountName's stringValue()
    set passwordName to passwordName's stringValue()
end setCredentialsFromUserInput_

我将OK按钮与上述IBAction以及上面关联的IBOutlets的ID和密码字段相关联。该项目构建并运行正常,但是当我单击“确定”按钮时,没有任何反应。它不会继续执行项目中的其余代码。我想我错过了一些基本的东西 - 想法?

更新#1:

这是脚本中的其余代码。其中大部分是从AppleScript Studio编写的这个应用程序的早期工作版本导入的(我相信)。我希望你也会在这里找到其他错误,但我主要担心的是当我点击OK时似乎没有任何进展。

-- Set text delimiters
-- From previous version of Map Drives application
on removechar(delim, sourcetxt) --remove delim from string source    
    try
        set OldDelims to AppleScript's text item delimiters -- save their     current state
        set AppleScript's text item delimiters to delim -- declare new delimiters
        set sourcelist to (every text item in sourcetxt) as list
        set sourcetxt to ""
        repeat with i from 1 to count of sourcelist
            set this_item to item i of sourcelist
            set sourcetxt to sourcetxt & this_item
        end repeat
        set AppleScript's text item delimiters to OldDelims -- restore them
        return sourcetxt
    on error
        set AppleScript's text item delimiters to OldDelims -- restore them in     case something went wrong
        end try
end removechar

on replacechar(delim, sourcetxt, replacement) --replace delim in string source     with replacement
    try
        set OldDelims to AppleScript's text item delimiters -- save their current state
        set AppleScript's text item delimiters to delim -- declare new delimiters

        set sourcelist to (every text item in sourcetxt) as list
        set sourcetxt to ""
        repeat with i from 1 to count of sourcelist
            set this_item to item i of sourcelist
            if i is equal to 1 then
                set sourcetxt to this_item
            else
                set sourcetxt to sourcetxt & replacement & this_item
            end if
        end repeat

        set AppleScript's text item delimiters to OldDelims -- restore them
        return sourcetxt
    on error
        set AppleScript's text item delimiters to OldDelims -- restore them in case something went wrong
    end try
end replacechar

-- Execute LDAP Query
on mapDrives_(accountname, passwordname)

    --setup ldap query
    set ldap_host to "LDAP HOST"
    set ldap_searchbase to "LDAP QUERY"
    set ldap_attribute to "NetworkResource"
    set query to "uid=\"" & accountname & "\""
    -- run ldap query

    try
        set networktest to do shell script "ping -c 1 " & ldap_host
    on error
        display alert "Unable to connect to server.

            Please check your internet connection and try again." as warning     default button "OK"
        restoreButton()
        return
    end try
    try
        set ldapresults to do shell script "ldapsearch -x -b " & ldap_searchbase & " -H ldap://" & ldap_host & " -D \"" & uName & "\"@" & ldap_host & " -w \"" & pWord & "\" -LLL " & query & " " & ldap_attribute & ""
    end try

    --Clean LDAP results
    --display dialog "results: " & ldapresults
    set cleanldapresults to removechar(space, ldapresults)
    --quit
    --display dialog "first pass: " & cleanldapresults
    set cleanldapresults to removechar(return, cleanldapresults)
    --display dialog "second pass: " & cleanldapresults
    set cleanldapresults to replacechar("\\", cleanldapresults, "/")

    --try
    set OldDelims to AppleScript's text item delimiters -- save their current state
    set AppleScript's text item delimiters to return -- declare new delimiters
    --separate out the cifs paths and load them into a list
    --display dialog "last pass: " & cleanldapresults
    try
        set mountpaths to (every text item in (do shell script "echo \"" & cleanldapresults & "\" | grep -o cifs_path=\"[^,]*\"")) as list
    on error
        display alert "Invalid drive mappings." as warning
        quit
    end try

    --display dialog mountpaths
    --iterate through mount paths and mount them
    repeat with i from 1 to count of mountpaths
        set this_item to item i of mountpaths
        set this_item to replacechar("cifs_path=", this_item, "")

        if this_item is not equal to "" then
            try
                -- display alert "Mapping: smb:" & this_item
                mount volume ("smb:" & this_item) as user name uName with password pWord
            on error
                display alert "Failed to map: smb:" & this_item & " with username: " & uName as warning
            end try
        end if

    end repeat
    display alert "Your network drives have been mapped." as informational default button "OK"
    restoreButton()
    set AppleScript's text item delimiters to OldDelims -- restore them
    quit

end mapDrives_

##################################################
# Application
on applicationWillFinishLaunching_(aNotification)
-- Insert code here to initialize your application before any files are opened 
end applicationWillFinishLaunching_

on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits 
return current application's NSTerminateNow
end applicationShouldTerminate_

end script

Answers:


1

你很亲密我认为问题在于您为文本字段创建了两个属性(accountName和passwordName),然后从这些文本字段中获取stringValues,然后尝试将这些字符串值存储回自身。

如果将值存储在单独的变量中,则代码可以成功运行。

 -- IBOutlets   
 property theWindow : missing value
 property accountName : missing value
 property passwordName : missing value

 -- IBActions (button clicks)
 on setCredentialsFromUserInput_(sender)
     set uname to accountName's stringValue()
     set pword to passwordName's stringValue()
     display alert "Username is " & uname & "\n\nPassword is " & pword
 end setCredentialsFromUserInput_

显示警告指令就在那里,因此您可以看到单击“确定”时发生的事情。


感谢那。我是否需要在声明名称和pword变量之上?一个例子如下所示:< property uname:“”>
Bryan Powell

我认为你不需要将它们声明为属性。我认为您只需要这样做,以便在构建界面时将它们作为出口提供。这些只是猜测。在看到你的问题之前我甚至都不知道AppleScriptObjC是一个东西,所以我不应该被认为是专家。 :)
Alistair McMillan

这是我的非程序员的想法不足的地方。我会玩更多。谢谢你让我走了。如果我有进一步的询问(非常可能),我应该提出一个新问题。再次感谢。
Bryan Powell

好吧,我实际上将继续这个主题,因为最初的问题是有效的。我创造了新的 uNamepWord 变量并将它们放在执行LDAP查询所需的代码中,但在单击OK后,项目中的其余代码不会执行。它似乎是创建变量但不会继续前进。
Bryan Powell

如果您将“显示警报”部分放入,是否会弹出正确的值?
Alistair McMillan
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.