Saving with JavaScript (widgets)

Discussion in 'Mac Programming' started by mrzeve, Feb 8, 2007.

  1. macrumors 6502a

    Joined:
    Jan 25, 2005
    #1
    Hey guys.

    I was wondering if anyone had any experience with saving information through javascript.

    For example, I'm developing a widget, and I want to add the ability for the user to be able to save the text of the story they are reading.

    It doesn't have to automatically save, I'm fine with a dialog box can pop up asking them where/if they would like to save the file.

    Any help at all would be appreciated. Just need to be steered in the right direction.

    Thanks a bunch.
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    The general JavaScript environment (in a web browser) explicitly can't write to local files at all. You might have some luck as you are in Dashboard but I suspect it'd be better to write the save component in Cocoa and link that to your widget (which is easily possible).
     
  3. thread starter macrumors 6502a

    Joined:
    Jan 25, 2005
    #3
    Thanks a bunch for that. I've seen many widgets that have the ability to save and I guess thats the way they do it. Plugins.

    Now I need to learn Cocoa and figure out how to implement it into the widget :rolleyes:
     
  4. macrumors member

    Joined:
    Oct 27, 2003
    Location:
    Herefordshire, UK
    #4
    There's nothing to stop you from using

    Code:
    widget.setPreferenceForKey(preference, key)
    widget.preferenceForKey(key)
    to store strings. AFAIK.
     
  5. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #5
    That stores the data in the user preferences, a binary or text xml file in ~/Library/Preferences. This should not be used to store anything other than user preferences...
     
  6. thread starter macrumors 6502a

    Joined:
    Jan 25, 2005
    #6
    So I realized what I wanted could be done a lot easier with AppleScript than in JavaScript.

    So I successfully created a .scrpt file and then learnt that I needed to use Xcode to create a bundle for the widget.

    So I simply created a Carbon (or was it Cocoa?) bundle project and added the Applescript to it. Then I changed the wrapper extension from .bundle to .widgetplugin.

    I then added it to the widget (note; I'm using Dashcode) and thought I could call it successfully, but I'm kind of confused on the calling process....anyone know how its supposed to be done? My AppleScript has several functions in it, how do I call them, and also, am I calling by the scripts name it self or do I simply call the function name straight up from the widget?

    Any help would be great
     

Share This Page