0 Replies Latest reply on Jan 3, 2011 10:53 AM by eplicanic

    [js] How can I close then open library or refresh library panel

    eplicanic

      I have a default library called: default.indl.

       

      I wrote a script that modifies the default.indl library and asks users to save modified version with their filename. The library is saved and opened in the panel. That works, UNTIL user resaves the library with same name.

       

      For example, if they save it as MyLibrary.indl then run the script again and resave it as MyLibrary.indl, opened library doesn't reflect the changes. If I close the library panel and re-open newely saved library the changes script made are there.

       

      So the changes work. Its just that they are not reflected in the library panel until reloaded.

       

      My stab at it was to:

       

      Delete existing library

      Recreate it

      Reload it

       

      Here is the code:

       

      var saveMyLibraryAs = function(){
      
      
      /* = CREATE DIALOG
      -------------------------------------*/
      var myLabelWidth = 100;
      var myTextBoxWidth = 200;
      var myDialog = new Window("dialog","Save Library As...");
      //var myPageNames = myGetPageNames();
      
      myDialog.orientation = "row"
      with(myDialog){
           var dialogPanel = add('panel', undefined,"");
           with(dialogPanel){
                orientation = 'column';
                alignChildren = 'left';
                margins = [12, 12, 12, 12];
                spacing = 8;
        
                with(add('group')){
                     orientation = 'row';
                     alignChildren = 'left';
                     var myLibraryName_0 = add("statictext", undefined, "New Library Name:");
                     myLibraryName_0.minimumSize.width = 120;
                     var myLibraryName = add("edittext", undefined, myDocWidth+' x '+myDocHeight);
                     myLibraryName.minimumSize.width = 200
                }
                
            }
             var buttons = myDialog.add('group');
                     buttons.orientation = 'column';
                     buttons.alignChildren = 'left';
                     buttons.add('button', undefined, 'OK', {name: 'ok'}); 
                     buttons.add('button', undefined, 'Cancel', {name: 'cancel'}); 
            
       }
       /* = END CREATE DIALOG
      -------------------------------------*/
      
           
      var myResult = myDialog.show();
           if(myResult == 1){
                //Get new library name
                var editedName = myLibraryName.text + '.indl';
                
                //save in 
                var userHome = Folder('~').fsName.toString();
                
                var libsPath = userHome + '/MyLibraries/';
                //gat path to the folder
                var libsFolder = CreateFolder(libsPath);
                //open default library
                var openedLib = File(libsPath+'default.indl');
                
                      /* ???? THIS MAY BE THE PROBLEM CODE ????? */
                //remove library if it exists
                if(File(libsFolder  +'/'+editedName).exists){
                     File(libsFolder  +'/'+editedName).remove();
                }
                
                //copy default to the new 
                openedLib.copy(libsFolder  +'/'+editedName);
                
                      
                //Close library if opened.
                if(app.libraries.itemByName(editedName).isValid){
                       app.libraries.itemByName(editedName).close();
                }else{
                     //reopen library (IT DOESN'T WORK!!!!!)
                     app.open(File(libsFolder+'/'+editedName));
                }
                
      
           }
      }
      var CreateFolder = function(Directory) {
           var folderPath = new Folder(Directory);
           if (folderPath.exists == false) {
                try{
                folderPath.create();
                }catch(e){alert("Oops! There was a hickup!");}
           }
           return folderPath;
      }