0

I need to preface this with the statement that I am not a programmer, so I need to be talked down to. I have done what I described below using guides and lots of trial and error

I have a functioning AppleScript which has 2 prompts for a user name and password which then proceeds to mount some network volumes based on an LDAP lookup. I have created a new AppleScriptObjC Xcode 6.4 project and brought in the code from the working AppleScript. Using interface builder, I created a simple input window and linked it up with the following code:

-- 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_

I linked the OK button with the IBAction described above and the ID and password fields with the associated IBOutlets above. The project builds and runs ok, but when I click the OK button, nothing happens. It does not proceed to execute the rest of the code in the project. I assume I am missing something basic here—thoughts?

Update #1:

Here is the rest of the code in the script. Most of this was imported from an earlier working version of this app written in AppleScript Studio (I believe). I expect you will find other errors in here as well, but my primary concerns is why nothing seems to move along when I click 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
1

You are close. I think the problem is that you have created two properties (accountName and passwordName) for your text fields but then you are taking the stringValues from those text fields and then trying to store those string values back into themselves.

If you store the values in separate variables the code works successfully.

 -- 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_

The display alert instruction is just there so you can see that something happens when you click OK.

  • Thanks for that. Do I need to above declare name and pword variables? One example looked like this: <property uname : ""> – Bryan Powell Jul 10 '15 at 15:46
  • I don't think you need to declare them as properties. I think you only need to do this so they are made available as outlets when you are building your interface. Those are just guesses though. Until seeing your question I didn't even know AppleScriptObjC was a thing, so I shouldn't be considered an expert. :) – Alistair McMillan Jul 13 '15 at 0:15
  • This is where my non-programmer's mind falls short. I will play around some more. Thanks for getting me going. If I have further inquiries (very likely) I should probably make a new question. Thanks again. – Bryan Powell Jul 13 '15 at 16:00
  • Well, I actually am going to continue this thread, because the original issue stands. I created new uName and pWord variables and placed them in the code where they are needed to execute the LDAP query, but after clicking OK the rest of the code in the project does not execute. It seems to be creating the variables but won't move on. – Bryan Powell Jul 14 '15 at 12:17
  • If you put the "display alert" part in does it pop up with the correct values? – Alistair McMillan Jul 14 '15 at 12:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .