Recent Widgets


Register for DashboardWidgets

Recent Forums Posts

Partners


iCompositions

MacDesktops.net

RSS Showcase
RSS Comments
RSS Forums

This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies. Posted in: Widget Development

Open URL in Safari (regardless of user's default browser)?

Author Message
kepner



Joined: 19 Mar 2008
Posts: 3
Location: Pennsylvania

Posted: Mon Mar 24, 2008 - 10:55 am    Post subject: Open URL in Safari (regardless of user's default browser)? Reply with quote

Why? To login to a website so that my widget also gets logged in (Safari and Dashboard share cookies apparently).

Is it possible?
View user's profile Send private message Send e-mail Visit poster's website Widgets
Quill



Joined: 23 Feb 2007
Posts: 11

Posted: Fri Jul 11, 2008 - 5:48 am    Post subject: Opening a URL in Safari Reply with quote

Hello,

You could use AppleScript to open the URL in Safari.

The AppleScript code is as follows:
Code:

tell application "Safari"
   activate
   make new document with properties {URL:"http://www.mywebsite.com/"}
end tell

...where "http://www.mywebsite.com/" is the website URL that you want to open.

Paste that code into a new document in Script Editor (/Applications/AppleScript/Script Editor.app) and save it as a ".scpt" file (I would place the file inside the widget bundle).

Then, you can launch the script from your JavaScript code using Apple's "widget.system" command:
Code:

widget.system("/usr/bin/osascript OpenURL.scpt", null);

...where "OpenURL.scpt" is the path to the AppleScript file that you created (in this case, it is just the file's name, since I'm assuming that both the AppleScript and the JavaScript files are in the same folder - the widget bundle).

Also, make sure that you have the following key in your Info.plist file:
Code:

   <key>AllowSystem</key>
   <true/>

...otherwise, the "widget.system" command won't work.

For more information on the "widget.system" command, see Apple's Documentation here. For more information on the "osascript" command (the command that you use to launch an AppleScript from the command line), see here.

Quill
View user's profile Send private message Send e-mail Visit poster's website Widgets
kepner



Joined: 19 Mar 2008
Posts: 3
Location: Pennsylvania

Posted: Sat Aug 02, 2008 - 12:44 pm    Post subject: Reply with quote

Thanks a lot, Quill!

Edit:

Wait a minute! If I can run Terminal commands in my widget, why bother with AppleScript?

Code:
widget.system("open \"http://example.com\" -a /Applications/Safari.app", null);


This actually works better, because if Safari isn't already open, the AppleScript results in two open Safari windows: a blank one, and one with your URL. The Terminal command only opens one Safari window, and only if the URL isn't already open in another window.

I still have a problem, and maybe I can live with it: when my Javascript function is called, and Safari opens with its one window, Dashboard remains active. Obviously, the user will see a new Safari window open, and will likely realize that they have to click on it.

Is there a Terminal command to deactivate Dashboard?
View user's profile Send private message Send e-mail Visit poster's website Widgets
Quill



Joined: 23 Feb 2007
Posts: 11

Posted: Sat Aug 02, 2008 - 9:56 pm    Post subject: Exiting Dashboard Reply with quote

Quote:
Wait a minute! If I can run Terminal commands in my widget, why bother with AppleScript?

You're right! I'm afraid that I don't know that much about Terminal commands... I'd better start learning a few more of them!

Consequently, I'm afraid I won't be able to tell you whether there is a Terminal command to exit Dashboard or not, but a recent thread here on Apple's Dashboard development mailing list suggested calling "widget.openURL" and passing it a blank string (rather than a url string):
Code:

if (window.widget)
{
   widget.openURL("");
}


It exits Dashboard to open the URL, but since there isn't a URL to open, it then does nothing... I tested it, and it seems to work well.

Quill


PS: I've been reading the Dashboard-Dev mailing list since my last post, and Apple doesn't want us to use "widget.system" synchronously, except for testing purposes (the command runs synchronously when we don't pass it a function to execute when finished).

So if the "widget.system" command returns something, instead of this:
Code:

var result = widget.system("[Terminal command that returns a value]", null).outputString;
// Code that works with "result" variable


...you might write it like this:
Code:

widget.system("[Terminal command that returns a value]", function() {
   var result = this.outputString;
   // Code that works with "result" variable
};



Otherwise, I guess we just pass it an empty function:
Code:

widget.system("[Terminal command]", function() {});
View user's profile Send private message Send e-mail Visit poster's website Widgets
kepner



Joined: 19 Mar 2008
Posts: 3
Location: Pennsylvania

Posted: Tue Aug 05, 2008 - 8:01 pm    Post subject: Reply with quote

Code:
 widget.openURL("");


That did it! Works great now!

Code:
widget.system("[Terminal command]", function() {});


Since Apple recommends it, I've implemented this as well.

Thanks for your help, Quill!
View user's profile Send private message Send e-mail Visit poster's website Widgets
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.

 
Powered by phpBB © 2001, 2002 phpBB Group