Text field help...

Can you explain? Not now, my dad sadly died, but can you explain tomorrow?

I am sorry to hear that.

Thank you. יברך שלום.

Ok, can you explain what you mean?

Here are a few thoughts.

First is to refactor your code and move all code that is repeated into their own methods.
This will make your code more readable and easier to maintain.

Use user defaults instead of do shell script.
If this app is for personal use then storing passwords in the plist file is fine. If you
plan on providing this app to others then you should use the keychain instead.

Stefan has pointed out in another post.
EMKeychain is a cocoa wrapper for the Carbon Keychain API.

This looks very easy to use.

As an example in using defaults. There are other examples in this forum as well.


set defaults to NSUserDefaults's standardUserDefaults
setTheusername_(defaults's stringForKey_("User"))
setTheuserpass_(defaults's stringForKey_("Pass"))

The html returned from this page says to not scrape their website so do not use the following code.


set tinyURL to do shell script "curl  " & quoted form of ("http://tinyurl.com/create.php?url=" & theURL) & " | grep '<blockquote><b>http://tinyurl.com/' | awk -F'<b>' '{print $2}' | awk -F'</b>' '{print $1}'" as string

Instead you could use NSString. There are other methods for talking to websites but since this specific
activity is so simple, NSString will be fine.


property |NSURL| : class "NSURL"
property NSString : class "NSString"

set userurl to "http://allancraig.net"
set urlString to "http://tinyurl.com/api-create.php?url=" & userurl

set theURL to |NSURL|'s URLWithString_(urlString)
set shortenedURL to NSString's stringWithContentsOfURL_encoding_error_(theURL, NSUTF8StringEncoding of current application, missing value)

Attach this handler to the TinyURL! button. You will first need to make that a button instead of a text field.


on openShortenUrlWindow_(sender)
	set {theLoc, theLen} to ACHelpers's rangeOfTextFieldSelection_(theTweet) as list
	tinyurlWindow's makeKeyAndOrderFront_(me)
end openShortenUrlWindow_

You can use this to replace the selected text after retrieving the tiny url


ACHelpers's replaceTextInTextfield_inRange_withString_(theTweet, {theLoc, theLen}, tinyURL)

Add this to ACHelper.m and the declaration in the .h file.

I would also suggest prompting for a username and password upon launch if they are missing. The application
should not attempt to post to Twitter if there is no username and password.

Regards,

Craig

Actually, there is no need to use the NSString example to get the tiny url. Using do shell script is easier but you still need to use the api code.

set userurl to "http://provided_url.com"
set urlString to "http://tinyurl.com/api-create.php?url=" & userurl

Thanks! I’ll get to that later, it sounds complicated. :smiley:

I will make a test app and try this out. Hope it works, and it looks hard. :smiley:

I am trying to put the cursor feature into my app. Here is my problem:

OK, when I do the line:

ACHelpers's replaceTextInTextfield_inRange_withString_(theTweet, {theLoc, theLen}, tinyURL)

The problem is that it will insert the TinyURL AFTER running the action connected to the text field “theTweet.” I don’t want or knew that line to run the action connected to the text field “theTweet” before inserting the text into the text box.

That is because you have the text field executing the filter handler. Remove that in IB.

Ok. I removed the action connected to the text field and then I made the buttons shortcut return so that when your press return, it makes it seem like its connected to an action. DONE! Now I need to add the keychain access. So close… falls

Craig, now the user defaults is doing the same thing, maybe worse, in ASOC then doing a shell script. I’m sticking with shell script because doing the user defaults in ASOC is worse and is more then just one line of code. I might try to encrypt the password though…

Not sure what you might be doing wrong but NSUserDefaults is the standard for saving preferences on OS X. I have never had any issues with it. Post the code that is giving you problems. This is definitely the way to go over do shell script for preferences.

One thing you might check. I noticed that you had altered the Bundle Identifier and removed the variable replacement at the end.

You removed everything after com.yourcompany. in the line below. This is needed for NSUserDefaults to
find the preferences file.

If you did this again in your new project, create another project and copy the Bundle Identifier from that application
into the working one.

Make it look something like this. Do not add the application name. Xcode will add that for you during the build.

OK, I looked on the post the StefanK has posted and I went through the diffrent types and nothing worked. Heres the code:

script NSString_and_Defaults_testingAppDelegate
	property parent : class "NSObject"
	property userfield : missing value
	property passfield : missing value
	property ACHelpers : missing value
	property NSUserDefaults : class "NSUserDefaults" of current application
	property NSMutableArray : class "NSMutableArray" of current application
	
	on awakeFromNib()
		set Newarraydata to NSMutableArray's alloc()'s initWithContentsOfFile_(POSIX path of (path to preferences folder from user domain as Unicode text) & "com.manifest.test.plist")
		if Newarraydata is not equal to missing value then
			tell NSUserDefaults
				tell its standardUserDefaults()
					set User to its objectForKey_("Username")
					my setUser_(User)
					set Pass to its objectForKey_("Password")
					my setPass_(Pass)
				end tell
			end tell
			userfield's setStringValue_(User)
			passfield's setStringValue_(Pass)
		end if
	end awakeFromNib
	
	on run_(sender)
		set Newarraydata to NSMutableArray's alloc()'s initWithContentsOfFile_(POSIX path of (path to preferences folder from user domain as Unicode text) & "com.manifest.test.plist")
		if Newarraydata is equal to missing value then
			set User to userfield's stringValue() as string
			set Pass to passfield's stringValue() as string
			tell NSUserDefaults
				tell its standardUserDefaults()
					its setObject_forKey_(User, "Username")
					its setObject_forKey_(Pass, "Password")
				end tell
			end tell
		end if
		set Newarraydata to NSMutableArray's alloc()'s initWithContentsOfFile_(POSIX path of (path to preferences folder from user domain as Unicode text) & "com.manifest.test.plist")
		if Newarraydata is not equal to missing value then
			tell NSUserDefaults
				tell its standardUserDefaults()
					set theUser to its objectForKey_("Username")
					my settheUser_(theUser)
					set thePass to its objectForKey_("Password")
					my setthePass_(thePass)
				end tell
			end tell
		end if
		display dialog theUser & " and " & thePass
	end run_
end script

And yes, the name of the bundle identifier is called “com.manifest.test”. Please help and post some code that you made.
(Sorry for the late respond, I was at my dads funeral.)

Dylan,

See this thread about defaults.

http://macscripter.net/viewtopic.php?id=30476

My message # 4 has the simple way to use defaults. I don’t think you have to check for the existence though you can set initial values with a check for the “first run.”


property NSUserDefaults : class "NSUserDefaults" of current application -- I always put these at the top

script NSString_and_Defaults_testingAppDelegate
   
tell NSUserDefaults's standardUserDefaults()
	set defaultsRegistered to its boolForKey_("defaultsRegistered")

	if (not my defaultsRegistered) then
		set defaults to {default0ne:"hello", defaultTwo:"someTextHere", defaultThree:"someTextThere"}
		registerDefaults_(defaults)
		set my defaultsRegistered to true
	else

--do your read in code here

end tell

end script

Sorry to hear about your dad.

Rob

I’ll see what I can do later. It also, seems complicated.
Thanks,

dylanweber

It really isn’t any different from the ASS way

tell user defaults
		if default entry "myEntry" exists then
			set myEntry to contents of default entry "myEntry"
		else
			make new default entry at end of default entries with properties {name:"myEntry", contents: myEntry}
		end if
end tell

vs.

tell NSUserDefaults's standardUserDefaults()
set myEntry to its stringForKey_("myEntry")
end tell

You’ll work it out. Also, maybe it isn’t the time for this today. Rob

Does anyone know what is the benefit is to use user defaults than a shell script?

It would help; Dylan Weber – dylanweber

The benefit is simple: “Native” methods are always preferable for timing and convenience reasons.

Ehhh… After a long time, “Native” and convenience doesn’t cut it… and Shell scripting is only ONE LINE!