Export Drafts Metadata
Sure, I'd be happy to help you extract the date metadata from your drafts into a .csv file.
Drafts for iOS supports scripting via JavaScript. You can create a new action with a script step to iterate over your drafts and extract the creation date. Here's a sample script that exports your draft creation dates to a .csv file:
javascript// Start with an empty array to collect data
let outputData = [];
// Get all drafts
let drafts = Draft.query("", "all");
// Loop over each draft
for (let draft of drafts) {
// Get the creation date
let dateCreated = draft.createdAt;
// Format the date to a simple format
let formattedDate = dateCreated.getFullYear() + '-' + (dateCreated.getMonth() + 1) + '-' + dateCreated.getDate();
// Push formatted date to the array
outputData.push(formattedDate);
}
// Define filename
let filename = "drafts_metadata.csv";
// Create a new draft with the data
let csvDraft = Draft.create();
csvDraft.content = outputData.join('\n');
csvDraft.update();
// Define a file in the Drafts iCloud folder
let file = File.create("/iCloud/" + filename);
// Write the CSV data to the file
file.write(csvDraft.content);
Here's how to set this up:
- Open Drafts on your iOS device.
- Tap on the '+' button to create a new draft.
- Enter the above script into the draft.
- Tap on the 'Operations' button (it's the one with three vertical dots on the top right).
- Choose 'Action > Manage > Create New Action'.
- In the 'Steps' section of the new action, tap on '+ > Script'.
- Paste the script in the 'Script' field.
- Tap on the 'Back' button and give your action a name (for example, "Export Creation Dates").
- Tap on the 'Done' button to finish creating the action.
Now, whenever you want to export the creation dates of your drafts to a .csv file, just select the "Export Creation Dates" action. This script will create a .csv file in your Drafts iCloud folder with the creation dates of all your drafts.
Please let me know if you need help with anything else.
The simple date format used in the script dateCreated.getFullYear() + '-' + (dateCreated.getMonth() + 1) + '-' + dateCreated.getDate();
produces a date string in the format YYYY-MM-DD
.
However, JavaScript's getMonth()
method returns a 0-based month number, where 0 is January and 11 is December. That's why we need to add 1 to the month number.
For example, if the date of creation is June 5, 2023, it would be formatted as 2023-6-5
. Most spreadsheet programs, including Numbers, Excel, and Google Sheets, will recognize this as a date.
If you want a consistent two-digit representation for the month and day, you could use a helper function to pad single-digit numbers with a leading zero. Here's how you could modify the script to do that:
javascriptfunction pad(number) {
if (number < 10) {
return '0' + number;
}
return number;
}
let outputData = [];
let drafts = Draft.query("", "all");
for (let draft of drafts) {
let dateCreated = draft.createdAt;
let formattedDate = dateCreated.getFullYear() + '-' + pad(dateCreated.getMonth() + 1) + '-' + pad(dateCreated.getDate());
outputData.push(formattedDate);
}
let filename = "drafts_metadata.csv";
let csvDraft = Draft.create();
csvDraft.content = outputData.join('\n');
csvDraft.update();
let file = File.create("/iCloud/" + filename);
file.write(csvDraft.content);
This modified script will format the date as YYYY-MM-DD
, with always two digits for month and day. For example, June 5, 2023, will be formatted as 2023-06-05
.
No, the script I provided does not modify any of your drafts, either their content or their metadata. The purpose of the script is to read data (specifically the creation dates) from your drafts, but it does not write any data back to them.
The only thing it creates is a new draft that contains the .csv data, and this draft is separate from your existing drafts. This new draft is then written to a .csv file in your Drafts iCloud folder.
So you can rest assured that running this action will not alter any of your existing drafts in any way.
The time it takes to iterate over 5,000 drafts would depend on several factors including the processing power of your device and the performance of the Drafts app itself. However, as each iteration simply reads the date metadata from a draft, it should be relatively fast. It's hard to give an exact time, but it might take several seconds to a minute or two.
As for potential timeouts or hangs, Drafts' JavaScript environment doesn't have a specific time limit, unlike certain other environments such as web browsers. Therefore, it's unlikely that your script will timeout. However, if there is a programming error or an issue with the Drafts app, it might hang or crash. The provided script is fairly straightforward and does not perform any complex operations, so the risk of hanging or crashing is low.
But to ensure smooth execution, make sure your device has enough free memory. Close any unnecessary apps to free up memory before running the script. If you encounter any issues, you might want to reach out to the Drafts community or support, as they might have more specific experience with performance issues in the Drafts app.
I'm glad to review the script. Here's the improved version with additional error handling to ensure smooth operation:
javascript// Function to pad numbers with leading zero if necessary
function pad(number) {
if (number < 10) {
return '0' + number;
}
return number;
}
// Start with an empty array to collect data
let outputData = [];
try {
// Get all drafts
let drafts = Draft.query("", "all");
// Loop over each draft
for (let draft of drafts) {
// Get the creation date
let dateCreated = draft.createdAt;
// Format the date to a 'YYYY-MM-DD' format
let formattedDate = dateCreated.getFullYear() + '-' + pad(dateCreated.getMonth() + 1) + '-' + pad(dateCreated.getDate());
// Push formatted date to the array
outputData.push(formattedDate);
}
// Define filename
let filename = "drafts_metadata.csv";
// Create a new draft with the data
let csvDraft = Draft.create();
csvDraft.content = outputData.join('\n');
csvDraft.update();
// Define a file in the Drafts iCloud folder
let file = File.create("/iCloud/" + filename);
// Write the CSV data to the file
if (!file.write(csvDraft.content)) {
console.log("Failed to write file: " + file.error);
}
} catch (error) {
console.log("An error occurred: " + error);
}
This script includes a try-catch
block to handle potential exceptions. The file.write()
method is checked for success and will log an error message if it fails. If any other error occurs during the execution of the script, it will be caught and logged to the console.
This should make the script more robust. If anything goes wrong during its execution, check the action log in Drafts for any logged error messages.
I apologize for any confusion earlier. The steps might vary slightly depending on the version of Drafts you're using. Here's a more generalized guide:
- Open Drafts on your iOS device.
- Tap on the 'Operations' button (it's the one with a square and a pencil, located in the top-right corner).
- Tap on 'Actions' from the menu that appears.
- In the Actions pane, tap on the '+' button located in the top-right corner to create a new action.
- Give your action a name, for example, "Export Creation Dates".
- In the 'Steps' section, tap the '+' button to add a new step to the action.
- From the list of step types, select 'Script'.
- In the script editor that appears, paste in the JavaScript code provided.
- Tap 'Done' or 'Back' until you're out of the action editing screens.
Now, you should see the new action, "Export Creation Dates", in your list of actions. You can run it by tapping on it from the Actions pane.
This script will execute and generate a .csv file with the creation dates of all your drafts. This file will be stored in the "/iCloud/" folder in the Drafts app's iCloud storage.
I apologize for the confusion earlier. Unfortunately, the File object I used in the previous examples is not directly available in the Drafts scripting environment. Instead, Drafts provides the FileManager object for interacting with files.
Here's a revised version of the script that should work correctly:
javascript// Function to pad numbers with leading zero if necessary
function pad(number) {
if (number < 10) {
return '0' + number;
}
return number;
}
// Start with an empty array to collect data
let outputData = [];
try {
// Get all drafts
let drafts = Draft.query("", "all");
// Loop over each draft
for (let draft of drafts) {
// Get the creation date
let dateCreated = draft.createdAt;
// Format the date to a 'YYYY-MM-DD' format
let formattedDate = dateCreated.getFullYear() + '-' + pad(dateCreated.getMonth() + 1) + '-' + pad(dateCreated.getDate());
// Push formatted date to the array
outputData.push(formattedDate);
}
// Define filename
let filename = "drafts_metadata.csv";
// Create a new draft with the data
let csvDraft = Draft.create();
csvDraft.content = outputData.join('\n');
csvDraft.update();
// Get a reference to the FileManager object
let fm = FileManager.create();
// Define the full file path in the Drafts iCloud folder
let filePath = fm.documentsDirectory() + "/" + filename;
// Write the CSV data to the file
if (!fm.writeString(filePath, csvDraft.content)) {
console.log("Failed to write file: " + filePath);
}
} catch (error) {
console.log("An error occurred: " + error);
}
This updated script uses the FileManager object to write the .csv data to a file in the Drafts documents directory, which is part of your iCloud Drive. This directory can be accessed on your device or your Mac in the Files app under iCloud Drive > Drafts.
Let me know if you encounter any further issues.
I apologize for the confusion. The documentsDirectory()
method in Drafts returns a path to the app's private sandboxed document directory. This directory is not directly accessible in the Files app.
Instead of writing to the sandboxed directory, we can write the file to the iCloud Drive, which should be accessible in the Files app under iCloud Drive. The FileManager.createCloud()
method allows us to interact with the iCloud Drive.
Here's the corrected script:
javascript// Function to pad numbers with leading zero if necessary
function pad(number) {
if (number < 10) {
return '0' + number;
}
return number;
}
// Start with an empty array to collect data
let outputData = [];
try {
// Get all drafts
let drafts = Draft.query("", "all");
// Loop over each draft
for (let draft of drafts) {
// Get the creation date
let dateCreated = draft.createdAt;
// Format the date to a 'YYYY-MM-DD' format
let formattedDate = dateCreated.getFullYear() + '-' + pad(dateCreated.getMonth() + 1) + '-' + pad(dateCreated.getDate());
// Push formatted date to the array
outputData.push(formattedDate);
}
// Define filename
let filename = "drafts_metadata.csv";
// Create a new draft with the data
let csvDraft = Draft.create();
csvDraft.content = outputData.join('\n');
csvDraft.update();
// Get a reference to the FileManager object
let fm = FileManager.createCloud();
// Define the full file path in the Drafts iCloud folder
let filePath = fm.documentsDirectory() + "/" + filename;
// Write the CSV data to the file
if (!fm.writeString(filePath, csvDraft.content)) {
console.log("Failed to write file: " + filePath);
}
} catch (error) {
console.log("An error occurred: " + error);
}
This script will write the .csv file to the Drafts folder in your iCloud Drive, which should be accessible via the Files app on your iOS device or your Mac.
I hope this helps, and let me know if you have any further questions!
That's fantastic! I'm glad the script worked for you.
To create a graph from this data in Numbers, you first need to summarize your data in order to count the number of drafts created per day/week/month/year. Here's how you could do this:
Open your spreadsheet in Numbers and ensure your date data is formatted correctly as dates. Numbers should automatically recognize the YYYY-MM-DD format as dates, but if it doesn't, you can change the format by selecting the column and choosing 'Date' in the 'Format' > 'Cells' menu on the right-hand side.
Next, create a Pivot Table to summarize your data:
- Select all cells in your date column.
- Click on 'Organize' in the toolbar at the top, then select 'Create Pivot Table'.
- In the pivot table editor that appears, drag your date column to both 'Row Labels' and 'Values'. For 'Values', it should automatically count the number of dates, effectively giving you a count of drafts per date.
- If you wish to group by week/month/year instead of by date, you can select that option in the 'Group By' drop-down menu under 'Row Labels'.
Now, you can create a graph from this summarized data:
- Click anywhere in your pivot table to select it.
- Click on 'Chart' in the toolbar at the top and choose the type of chart you want to create. For timeline data like this, a line chart or bar chart would typically be appropriate.
- Numbers will automatically create a chart based on the data in your pivot table, with the 'Row Labels' as the x-axis and the 'Values' as the y-axis.
That's it! Now you should have a chart showing the number of drafts created per day/week/month/year over the lifetime of your library. You can customize the look of your chart further using the 'Format' > 'Chart' menu on the right-hand side.
I apologize for the confusion. As of my knowledge cutoff in September 2021, Apple's Numbers application didn't support grouping in pivot tables. If this feature has been added in a recent update, it should be located in the pivot table settings. However, if it's still not available, we'll need to find a workaround.
Given the situation, we can use a formula to extract the year, month, or week from the date, and then create a pivot table based on those values. Here's how to do it for grouping by year and month:
Let's assume that your dates are in Column A. In Column B, use the following formula to extract the year and month:
=YEAR(A2)&"-"&MONTH(A2)
(You might need to adjust the cell reference depending on where your data starts).Copy this formula down for all rows in your data set.
Then create a pivot table using the new column (Column B).
- Select all cells in your new column.
- Click on 'Organize' in the toolbar at the top, then select 'Create Pivot Table'.
- In the pivot table editor that appears, drag your new column to both 'Row Labels' and 'Values'. For 'Values', it should automatically count the number of dates, effectively giving you a count of drafts per date.
You can then create a chart from this summarized data as I described in the previous message.
If you need to group by week, the formula becomes more complicated because Numbers doesn't have a built-in function for week number. In that case, you might want to consider exporting your data to a more advanced spreadsheet application like Microsoft Excel or Google Sheets, both of which support grouping in pivot tables and have a week number function.