UXP accessing files and folders

Oh sorry I had forgotten. .

on your manifest 5 add this after icons.

 "requiredPermissions": {
        "localFileSystem": "fullAccess", // this is important for the access
      // with regards to the question the rest is not of much importance
        "network": {
            "domains": [
                "http://forums.creativeclouddeveloper.com/t/uxp-accessing-files-and-folders/5930/15",                        
            ]
        }, 
    "launchProcess": {
        "schemes": [
          "http",
          "https"
        ],
        "extensions": [
          ".svg",
          ".png",
          ".pdf",
          ".xd", 
          ".psd"
        ]
      },
       "allowCodeGenerationFromStrings": true        
    }

// code for reading file from disk


 function readFileFromDisk() {
  try {
    // make sure an active doc is opened
    // check if any file is open. if not return error
    let activeDocument = (PS.app.activeDocument as PS.Document) || null
    if (activeDocument === null) {
      return psCore.showAlert({ message: 'Please open a document' })
    }   

    // check if file is saved before proceeding
    if(!activeDocument.save){
      // request the user to save it 
//return an alert
}

    // fetching the file
 
    if (activeDococument.path === undefined) {
    // for some reasons am yet to understand the path is undefined for some files
      return psCore.showAlert('Error fetching file path.')
    }
    
    // read the file
    const fileBuffer: ArrayBuffer = await FS.readFile(`file:${activeDoc.path}`) // 

    // fetch the file's name
    let fileName: string = activeDocument.title

  console.log('filename', fileName,'  file Array Buffer ', fileBuffer) 

    
  } catch (error) {
    console.error(' Error : ', error)
   
  }
}
1 Like