App freezes up

I have some buttons on my app that will copy items to certain locations. When the open panel comes up and you hit the cancel button my app freezes up on me. All I get is a never ending spinning beach ball.

Can anyone help to resolve this?

here is the code for one of the buttons:


	on uploadToLocalAdTemplates_(sender)
		set manager to current application's NSFileManager's defaultManager()
		manager's setDelegate_(me)
		set textFieldAdValue to (textFieldAd's |stringValue|()) as text --gets textFields string value
		set inimg02 to "files_2:localadtemplates" --sets the destination folder
		if textFieldAdValue is "" then error display dialog "You need to put a season code in the box" --checks textField to see if it is empty and display error code
		tell application "Finder"
			if not (exists folder textFieldAdValue of folder inimg02) then --creates new folder in destination folder
				make new folder at inimg02 with properties {name:textFieldAdValue}
			end if
		end tell
		set cp to current application's NSOpenPanel's openPanel() --open panel for selecting files and folders
		cp's setCanChooseFiles_(1) -- 1 allows choosing files, 0 disallows
		cp's setCanChooseDirectories_(1) -- 1 allows choosing folders, 0 disallows
		cp's setAllowsMultipleSelection_(1) -- 1 allows multiple selection, 0 disallows
		cp's runModal()
		repeat with aURL in cp's URLs()
			set theChosenPath to aURL's |path|()
			set theChosen to aURL's lastPathComponent()
			set dstPath to current application's NSString's stringWithString_(POSIX path of (inimg02 & ":" & textFieldAdValue as string))
			set dstPath to dstPath's stringByAppendingPathComponent_(theChosen)
			log dstPath
			log manager's copyItemAtPath_toPath_error_(theChosenPath, dstPath, missing value) -- should log 1 if successful
		end repeat
	end uploadToLocalAdTemplates_

this appears to happen on every button that I click, and there is no errors being logged.

Thanks,
Scott

I copied and pasted your code into a new app, and I got the same behavior as you did, but what I observed wasn’t a really a freeze but that the copy command was trying to copy everything in my documents folder (which was the default location for my open panel when it opens). Since there is so much stuff in that folder, it would appear as a freeze if you aren’t logging anything. I implemented this:

on fileManager_shouldCopyItemAtPath_toPath_(man, atPAth, toPath)
		log "here"
		return 1
	end fileManager_shouldCopyItemAtPath_toPath_

This gets called before each attempted copy, and I saw an unending (until I quit that is) log of “here”.

When you click cancel, theURL will be whatever the URL of the location shown at the top of the panel is. This seems like a bad behavior for a panel to me, but that’s what it does. This problem can be fixed by checking the return value of the runModal command (1 for open, 0 for cancel) like this:

if cp's runModal() as integer is 1 then
			repeat with aURL in cp's URLs()
				set theChosenPath to aURL's |path|()
				set theChosen to aURL's lastPathComponent()
				set dstPath to current application's NSString's stringWithString_(POSIX path of (inimg02 & ":" & textFieldAdValue as string))
				set dstPath to dstPath's stringByAppendingPathComponent_(theChosen)
				log dstPath
				log manager's copyItemAtPath_toPath_error_(theChosenPath, dstPath, missing value) -- should log 1 if successful
			end repeat
		else
			log "User Cancelled"
		end if

Ric

O.K. that worked but…
When I hit the cancel button the open panel comes back, and then when I hit cancel a second time it finally goes away. It is almost like the panel does not go away unless I hit cancel twice.

Any ideas?

Thanks,
Scott

Hmm… I don’t get that behavior, so I’m not sure why it would reopen, maybe something elsewhere in your code?

Ric

Here is how I implemented your code…


	on uploadToLocalAdTemplates_(sender)
		set manager to current application's NSFileManager's defaultManager()
		manager's setDelegate_(me)
		set textFieldAdValue to (textFieldAd's |stringValue|()) as text --gets textFields string value
		set inimg02 to "files_2:localadtemplates" --sets the destination folder
		if textFieldAdValue is "" then error display dialog "You need to put a season code in the box" --checks textField to see if it is empty and display error code
		tell application "Finder"
			if not (exists folder textFieldAdValue of folder inimg02) then --creates new folder in destination folder
				make new folder at inimg02 with properties {name:textFieldAdValue}
			end if
		end tell
		set cp to current application's NSOpenPanel's openPanel() --open panel for selecting files and folders
		cp's setCanChooseFiles_(1) -- 1 allows choosing files, 0 disallows
		cp's setCanChooseDirectories_(1) -- 1 allows choosing folders, 0 disallows
		cp's setAllowsMultipleSelection_(1) -- 1 allows multiple selection, 0 disallows
		cp's runModal()
		if cp's runModal() as integer is 1 then
			repeat with aURL in cp's URLs()
				set theChosenPath to aURL's |path|()
				set theChosen to aURL's lastPathComponent()
				set dstPath to current application's NSString's stringWithString_(POSIX path of (inimg02 & ":" & textFieldAdValue as string))
				set dstPath to dstPath's stringByAppendingPathComponent_(theChosen)
				log dstPath
				log manager's copyItemAtPath_toPath_error_(theChosenPath, dstPath, missing value) -- should log 1 if successful
			end repeat
		else
			log "User Cancelled"
		end if
	end uploadToLocalAdTemplates_

As far as I can tell everything looks to be in order, so i am not sure how this is happening…

Thanks,
Scott

I don’t see anything in there either. Does “User cancelled” get logged once or twice?

Ric

it only gets logged once.

That would suggest that this piece of code is not responsible for opening the panel the second time (I’m assuming that it gets logged the first time you click cancel). Could there be somewhere else in your code that is causing this?

Ric

It does not get logged until the second time that you press cancel. And there should be nowhere else in my code that would effect this.

Did you try moving the open panel (before you click cancel the first time) to see if there is another one behind it?

Ric

After edit: oh, never mind – the problem is you left the runModal() line in as well as the one in the if statement.

Thanks that did it. I appreciate all your help.

Thanks,
Scott