0

I'm trying to make a shortcut to delete some files with the help of Scriptable app in iOS26.

I want to search inside a specific folder (iCloud Drive > Images) for JPEGs & if found delete all of them whose creation date is 3 days earlier than current day ?

Below is the scriptable script to do so:

// TARGET FOLDER PATH
const folderPath = "Images"; // iCloud Drive > Images

// Get current date and subtract 3 days
const now = new Date();
const threshold = new Date(now.getTime() - 3 * 24 * 60 * 60 * 1000);

// Load files from folder
const fm = FileManager.iCloud();
const folder = fm.joinPath(fm.documentsDirectory(), folderPath);
const files = fm.listContents(folder);

let deleted = [];

for (let file of files) {
  const fullPath = fm.joinPath(folder, file);

  // Check if it's a JPEG
  if (file.toLowerCase().endsWith(".jpg") || file.toLowerCase().endsWith(".jpeg")) {
    const attrs = fm.getAttributes(fullPath);
    const created = attrs.creationDate;

    // Compare creation date
    if (created <= threshold) {
      fm.remove(fullPath);
      deleted.push(file);
    }
  }
}

// Show result
if (deleted.length > 0) {
  let msg = `Deleted ${deleted.length} JPEG(s):\n` + deleted.join("\n");
  await new Alert({ title: "Cleanup Complete", message: msg }).present();
} else {
  await new Alert({ title: "No Files Deleted", message: "No JPEGs older than 3 days found." }).present();
}

But getting error

ERROR MSG loc

Code to list the folders accessible to Scriptable

const fm = FileManager.iCloud();
const root = fm.documentsDirectory();
const items = fm.listContents(root);

let folders = [];

for (let item of items) {
  const path = fm.joinPath(root, item);
  if (fm.isDirectory(path)) {
    folders.push(item);
  }
}

if (folders.length > 0) {
  let msg = "Folders Scriptable can access:\n" + folders.join("\n");
  await new Alert({ title: "iCloud Folders", message: msg }).present();
} else {
  await new Alert({ title: "No Folders Found", message: "Scriptable couldn't find any folders in iCloud Drive." }).present();
}const fm = FileManager.iCloud();
const root = fm.documentsDirectory();
const items = fm.listContents(root);

let folders = [];

for (let item of items) {
  const path = fm.joinPath(root, item);
  if (fm.isDirectory(path)) {
    folders.push(item);
  }
}

if (folders.length > 0) {
  let msg = "Folders Scriptable can access:\n" + folders.join("\n");
  await new Alert({ title: "iCloud Folders", message: msg }).present();
} else {
  await new Alert({ title: "No Folders Found", message: "Scriptable couldn't find any folders in iCloud Drive." }).present();
}
6
  • I’m not familiar with this app, but can you get it to list the contents of iCloud Drive? Commented Oct 28 at 14:59
  • @LincDavis I gave it a shot, but when I run it, it just hangs with that spinning loading icon. I've added that code as well in my post (edited). Commented Oct 28 at 15:04
  • Ah, on iOS. Sorry, missed that. Forget my remarks then. Commented Oct 28 at 15:42
  • I don’t think it can access iCloud Drive files, at least not that way. The files are not present on your device. Commented Oct 28 at 15:43
  • @LincDavis Then can it access On My iPhone files ? How ? Commented Oct 28 at 16:16

2 Answers 2

0

You might have better luck with a Shortcut. See, for example, Can iPhone delete files immediately, without sending them to Trash? . You would have to add a test for the age of the file.

0

Well it turns out that to make this work, you first need to bookmark the folder you're interacting with. Once bookmarked, you can access it using the appropriate FileManager methods. There is an option to bookmark a folder in the Settings of the app Scriptable called 'File Bookmarks'. Here's the complete code that's currently working for me.

// Get the iCloud file manager
const fm = FileManager.iCloud();

// Name of the bookmark and folder
const bookmarkName = "Images";

// Threshold (in milliseconds) for file age (3 days)
const THRESHOLD_MS = 3 * 24 * 60 * 60 * 1000; // 3 days

// Check if the bookmark exists
if (fm.bookmarkExists(bookmarkName)) {
  const folderPath = fm.bookmarkedPath(bookmarkName);
  const contents = fm.listContents(folderPath);

  // Get files older than 3 days by creation date
  let toDelete = [];
  const now = Date.now();
  for (const file of contents) {
    const filePath = fm.joinPath(folderPath, file);
    if (fm.fileExists(filePath)) {
      const created = fm.creationDate(filePath);
      if (created) {
        const age = now - created.getTime();
        if (age >= THRESHOLD_MS) {
          toDelete.push(filePath);
        }
      }
    }
  }

  let notification = new Notification();
  if (toDelete.length === 0) {
    notification.title = No Old Files;
    notification.body = No files in "${bookmarkName}" created 3+ days ago.;
  } else {
    // Delete old files
    let deletedCount = 0;
    for (const filePath of toDelete) {
      fm.remove(filePath);
      deletedCount++;
    }
    notification.title = Deleted Old Files;
    notification.body = Deleted ${deletedCount} file(s) from "${bookmarkName}" created 3+ days ago.;
  }
  notification.schedule();
} else {
  // Bookmark not found
  let notification = new Notification();
  notification.title = "Bookmark Not Found";
  notification.body = File bookmark "${bookmarkName}" does not exist in Scriptable settings.;
  notification.schedule();
}

Script.complete();

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.