Can I rename the current file using a workflow?
Hi. I'm completely loving Editorial 1.1. And I'm having fun experimenting with workflows. But I'm not such a savvy python coder... Here is a particular workflow I'm struggling with:
What I'd like to do is rename the current file (stored in dropbox) with an additional prefix. For instance, if the current dropbox file is named "my-document.txt", I'd like to change it to "archive-my-document.txt". Is this possible? To rename the current document?...
I'm thinking I'd use os.rename(src,dst) but I'm not sure what to plug in for the current filename. Do I want the entire path name or just relative to the root dropbox folder? Would I use editor.get_path() or os.path.basename(editor.get_path()) or the "Get Current File Name" action? None of this seems to work so far.
Can anyone help me figure out the code (or actions) necessary to get the correct filename in order to rename it?
Being intrigued by your question I looked through some API and found this entry:
editor.set_file_contents(name, new_contents[, root])
Writes new data to the file with the given name (a relative path). If a file with this name exists, it is overwritten.
Note that new_contents is a byte string, which means that you can also use this to write binary files (such as images), but if you write non-ASCII text, you have to encode it yourself.
root can be either ‘local’ or ‘dropbox’. ‘local’ is the default.
You can grab the exact and relative paths from the editor module and this may be what you are looking for.
Hope this helps :)
Oh. Cool. That didn't occur to me. Thanks. Plus, I see there are actually actions for "get file contents" and "set file contents". I don't have my iPad with me right now but I think this could work:
(Action) Get current file name -> Set variable
(Action) Get file contents -> Set variable
(Action) Set file contents (with new file name)
(Action) Open new file
(Python) Delete old file
Thanks for the feedback.
@whyzee I'm not able to delete a file. This is the error I get when I try to do this:
Here's my Python script:
#coding: utf-8 import workflow import os os.remove(workflow.get_variable('old_file'))
The old_file variable is derived from
File Extension. The actual name is correct but the path is what's wrong (I'm pretty sure). Here's the console error I get when I try to run this.
Traceback (most recent call last): File "/var/mobile/Containers/Data/Application/<ID>/Library/Application Support/Commands/workflow_action_tmp.py", line 5, in <module> os.remove(workflow.get_variable('old_file')) OSError: [Errno 2] No such file or directory: 'Untitled.md'
Have you successfully gotten file deleting to work in Editorial? I'm wondering if it's programmatically possible...
os.listdir('.')to see if the file is in the Current Working Directory.