I have real problem in displaying the content of the folders which are located inside the root directory. It managed to determine the folders which are in the Files directory but when I try to do the same to one of those folders It doesn't work.
I delieve the problem in the path name of a WL.api. However I may be mistaken.
I used code samples from skydrive page of live connect development center. in the sample below I tried to determine folders first, but eventually I would like to get the names of all files stored in a particular directory.
WL.api({ path: "me/skydrive/files/myfolder", method: "get" }).then(
function (response) {
var items = response.data;
var outPuts = "";
var number = items.length
var tempos = new Array();
var foundFolder = 0;
for (var i = 0; i < items.length; i++) {
if (items[i].type === "folder" || items[i].type === "album") {
tempos[i] = items[i].name;
foundFolder += 1;
}
}
if (foundFolder == 0) {
folderss.innerHTML = ("Unable to find any folders");
}
else {
for (var i = 0; i < number; i++) {
outPuts = outPuts + tempos[i] + "<br /> <br />"
}
folderss.innerHTML = outPuts;
}
}
);
if I retain only "me/skydrive/files" for WL path. it works. But if I add any particular folder name afer it like in my case "me/skydrive/files/myfolder" the call returns nothing. or may be I shall declare a path like: "me/skydrive/files/folder.567391047.34282821!"
Thank you for anyone who can help.
I believe your problem is due to the fact that you are using an invalid path format. According to the examples from the docs, a valid path to list files has the following form: /OBJECT_ID/files, where OBJECT_ID may be replaced by me/skydrive to reference the Skydrive root folder.
The important things to note are that:
there can be a reference (OBJECT_ID) to only one object;
this reference can only be the ID of an object (as returned by the API) or a special alias such as me/skydrive;
/files should always be the last part of the path (assuming we do not need to use a query string).
Thus, to list the contents of your subfolder folder.567391047.34282821!, you should try using the following path format instead:
/folder.567391047.34282821!/files or even folder.567391047.34282821!/files (without the leading slash, as it seems to be optional).
Please let me know if this solves your issue.
Related
I work as legal support in litigation. I am not that clued up on scripting, but have managed to adapt a few google searches to perform various tasks for in Adobe.
What I need is help with what I think should be a simple script to read through a PDF and extract Document IDs. They are enclosed in square brackets, so I just need to extract all text between square brackets to a text or CSV file. I have tried using the ChatGPT bot but that hasnt been very successful. This is the code it has given me
// Open the PDF file
var filePath = "/path/to/your/file.pdf";
var doc = app.open(filePath);
// Get the number of pages
var numPages = doc.numPages;
// Create an array to hold the results
var results = [];
// Loop through each page and extract text between square brackets
for (var i = 0; i < numPages; i++) {
var page = doc.getPageNthWordQuads(i);
for (var j = 0; j < page.length; j++) {
var word = page[j];
var text = word[4];
// Check if the text is between square brackets
if (text.startsWith("[") && text.endsWith("]")) {
// Remove the brackets and add the text to the results array
results.push(text.slice(1, -1));
}
}
}
// Save the results to a text file
var outputPath = "/path/to/your/output/file.txt";
var outputFile = new File(outputPath);
outputFile.open("w");
outputFile.write(results.join("\n"));
outputFile.close();
// Close the PDF file
doc.close();
I ran the script, with my file directory, not the placeholder in the script, but nothing happened. No error or anything
I am using a work PC so I cant install python or any other program, hence the need for Java or possibly powershell if that will work
Can anyone help me?
Actually I realised i could do this using the Evermap plugin. Highlight text by pattern - [(.*?)], then extract highlighted text.
This can be achieved with pdf.js library: below example shows if specific text in the first page but can be furhter extended to check the whole pdf. Hope this helps!
// Load PDF.js library
const pdfjsLib = require('pdfjs-dist');
// Load PDF file
const url = 'path/to/pdf/file.pdf';
const loadingTask = pdfjsLib.getDocument(url);
loadingTask.promise.then(function(pdf) {
// Load the first page
pdf.getPage(1).then(function(page) {
// Get the text content of the page
page.getTextContent().then(function(textContent) {
// Iterate through each text item
for (let i = 0; i < textContent.items.length; i++) {
const item = textContent.items[i];
// Check if the text item matches your criteria
if (item.str.includes('specific text')) {
console.log(item.str);
}
}
});
});
});
You can get rid of the entire for loop and just use a regular expression with String.match():
const data = "This is the text ofa pdf file [documentid1] and [documentid2].";
const matches = data.match(/(?<=\[).*?(?=\])/gs);
console.log(matches);
I'm trying to get my javascript to ignore one file type extension that's held in a folder with a bunch of photoshop images. For all of the other file types in the folder I have it so that these file types populate a window and the user can import into their work space.
I have modified my script to ignore the file extension I want ignored, however it no longer populates the window with all of the other file types containted in the folder. But when I take out the file I want ignore from the folder, the window gets populated as it should.
This is what I have at the moment that checks my folder for the file types:
//Prompt for folder location
var Path = Folder.selectDialog("Select Folder Location for Renders")
// Use the path to the application and append the samples folder
var samplesFolder = Folder(Path)
//Get the files
var fileList = samplesFolder.getFiles()
//Creat Array to hold names
var renderTypes = new Array();
//Parse Initial name to get similar render elements
var beautyRender = fileList[0].name
beautyRender = beautyRender.substr(0, beautyRender.length-4)
//Get the render elements with a similar name
for (var i = 0; i < fileList.length; i++)
{
var filename = fileList[i].name;
if (filename.match(/\.(stitchinfo)$/i) == null)
{
if(fileList[i].name.substring(0,beautyRender.length) === beautyRender)
{
renderTypes.push(fileList[i].name);
}
}
}
Can anyone see what I've done wrong and need to modify?
Update
I'm still trying to get this to work and following the help from one of the posters below I have modified my code to the following:
for (var i = 0; i < fileList.length; i++)
{
var filename = fileList[i].name;
if (filename.match(/\.(stitchinfo)$/i) == null)
{
renderTypes.push(fileList[i].name);
}
}
However, with this new code comes a new problem in that it returns every file contained in the folders and displays it.
I'm still stumped as to how I can get this to work as I would like. Please can anyone help me?
You're creating a sparse array, because you skip elements in renderTypes when you ignore a filename in fileList. That may be confusing your rendering code. Change to:
renderTypes.push(fileList[i].name);
What if :
for (var i = 0; i < fileList.length; i++)
{
var filename = fileList[i].name;
if (filename.match(/\.(stitchinfo)$/i) == null)
{
if(fileList[i].name.substring(0,beautyRender.length) === beautyRender)
{
renderTypes.push(fileList[i].name);
}
}
}
Wrong usage of the array
Missing ";"
Unnecessary use of "continue".
Managed to get a fix for this.
What I've ended up doing is creating a new function and passing that into my call for checking the folder locations.
function ignoreMe(f)
{
return f.name.match(/\.(stitchinfo)$/i) == null;
}
And the folder check:
var fileList = samplesFolder.getFiles(ignoreMe);
I am using the following code in order to retrieve sub folder names from the path declared. This works fine but how do I then remove the path name so that the array is a list of just folder names?
var myPath = Folder ("Z:/My File System/Me/Work Files/Design");
var folders = getFolders (myPath);
function getFolders(sourceFolder) {
var folderArray = new Array();
var sFolders = sourceFolder.getFiles ();
var len = sFolders.length;
for (var i = 0; i < len; i++) {
var sFolder = sFolders[i];
if (sFolder instanceof Folder) {
folderArray.push(sFolder);
}
}
return folderArray;
}
Instead of returning:
Z:/My File System/Me/Work Files/Design/One
Z:/My File System/Me/Work Files/Design/Two
Z:/My File System/Me/Work Files/Design/Three
Z:/My File System/Me/Work Files/Design/Four
I need:
One
Two
Three
Four
You could implement something like this (assuming you can modify the Folder prototype, and it stores the path as this.path):
Folder.prototype.basename = function () {
return this.path.split('/').pop();
};
You would then append the base names to the array:
folderArray.push(sFolder.basename());
You could use split() like this, assuming there are no other slashes towards the end.
var sample = 'Z:/My File System/Me/Work Files/Design/Four'.split('/')
var result = sample[sample.length - 1]
Regexp the string. Start at the end, and work backward until the first '/'
Working in Node I need to convert my request path into a relative path so I can drop it into some templates that have a different folder structure.
For example, if I start with the path "/foo/bar" and want the relative path to "/foo", it would be "..", and for "/foo/bar/baz" it would be "../..".
I wrote a pair of functions to do this:
function splitPath(path) {
return path.split('/').map(dots).slice(2).join('/');
}
function dots() {
return '..';
}
Not sure if this is the best approach or if it's possible to do it with a regular expression in String.replace somehow?
edit
I should point out this is so I can render everything as static HTML, zip up the whole project, and send it to someone who doesn't have access to a web server. See my first comment.
If I understand you question correct you can use path.relative(from, to)
Documentation
Example:
var path = require('path');
console.log(path.relative('/foo/bar/baz', '/foo'));
Node.js have native method for this purposes: path.relative(from, to).
This might need some tuning but it should work:
function getPathRelation(position, basePath, input) {
var basePathR = basePath.split("/");
var inputR = input.split("/");
var output = "";
for(c=0; c < inputR.length; c++) {
if(c < position) continue;
if(basePathR.length <= c) output = "../" + output;
if(inputR[c] == basePathR[c]) output += inputR[c] + "/";
}
return output;
}
var basePath ="/foo"
var position = 2;
var input = "/foo";
console.log(getPathRelation(position,basePath,input));
var input = "/foo/bar";
console.log(getPathRelation(position,basePath,input));
var input = "/foo/bar/baz";
console.log(getPathRelation(position,basePath,input));
Result:
(an empty string)
../
../../
So I've seen lots of scripts to grab the YouTube ID from a URL with JavaScript, but I can't find or figure out how to grab the ID along with any additional variables. I have a PHP script which can do it, but I'd like to do this with JavaScript. Does anyone know how this can be accomplished?
I doubt it's necessary but here are the two scripts I'm using
JavaScript for video ID...
url = $(this).text();
var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/;
var match = url.match(regExp);
if (match&&match[2].length==11){
alert(match[2]);
}else{
alert("not youtube");
}
And PHP...
if (preg_match('%(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match)) {
$youtubeid = $match[1];
$getyt = (parse_url($url));
}
if(strlen($getyt['fragment']) > 0) {
$addhash = '#' . $getyt['fragment'];
}
$embed = "http://www.youtube.com/embed/$youtubeid?wmode=opaque&autoplay=1$addhash";
The PHP one is pretty good, except that it only works for hash tags. My primary reason for wanting the additional variables is for when someone wants to specify a start time. So if anyone can tell me how I can grab the URL and additional paramaters (or just the specified starting time of the video) then I'd really appreciate it, because I'm absolutely lost.
This creates an associative array named video_parameters:
function extractParameters(url)
{
var query = url.match(/.*\?(.*)/)[1];
var assignments = query.split("&")
var pair, parameters = {};
for (var ii = 0; ii < assignments.length; ii++)
{
pair = assignments[ii].split("=");
parameters[pair[0]] = pair[1];
}
return parameters;
}
url = "http://www.youtube.com/watch?v=gkU&list=UUa3Q&feature=plcp";
video_parameters = extractParameters(url);
// video_parameters["v"]
// > "gkU"
// video_parameters["list"]
// > "UUa3Q"
See How can I get query string values in JavaScript? for methods that handle special chars.