# Need help with workflow\_script

**URL:** <https://help.nextcloud.com/t/need-help-with-workflow-script/95750>\
**Category:** ℹ️ Support\
**Created:** [October 20, 2020, 9:02pm UTC](https://help.nextcloud.com/t/need-help-with-workflow-script/95750 "2020-10-20T21:02:05Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![BruderB](https://help.nextcloud.com/letter_avatar/bruderb/32/6_22734026db12803ebb3e059622ac3534.png) [@BruderB](https://help.nextcloud.com/u/BruderB)\
**Post date:** [October 20, 2020, 9:02pm UTC](https://help.nextcloud.com/t/need-help-with-workflow-script/95750/1 "2020-10-20T21:02:05Z")

</div>

Hej NC-members,

unfortunatly, the documentation for the app workflow\_script is not complete - careful said.  
On the other hand it seems to be the best way to solve my idea:  
After creation (upload) of a jpg-file in a dedicated directory /files/jpg-upload the image shall be converted (with image-magick) into a lower resolution.

So I installed workflow\_script and tried build the trigger-rule:  
If “File created”  
and “Filename” “is” “???”  
How is the path to be formed? Absolute? Relative to what?  
Is it possible to use a wildcard?  
Is there a logfile?

And when this part is done, the next question would be how to put in the script that shall be executed. Where in the filesystem must that script be placed? Grants?

Is there anybody out there having this managed to work??

Thanks for help!

Boris

---

<div class="post-metadata">

**Author:** ![j-ed](https://help.nextcloud.com/user_avatar/help.nextcloud.com/j-ed/32/7281_2.png) [@j-ed](https://help.nextcloud.com/u/j-ed)\
**Post date:** [October 21, 2020, 9:37am UTC](https://help.nextcloud.com/t/need-help-with-workflow-script/95750/2 "2020-10-21T09:37:29Z")

</div>

> [@BruderB](#):
>
> unfortunatly, the documentation for the app workflow\_script is not complete - careful said.

As usual this depends on the point of view 😉

The best way to start is always to use the search function of this forum because most likely similar questions have already been asked and answered in the past:

[https://help.nextcloud.com/search?q=workflow\_scripts%20%40j-ed](https://help.nextcloud.com/search?q=workflow_scripts%20%40j-ed)

> If “File created”  
> and “Filename” “is” “???”

I think it’s a bad idea to rely on file names because they can be spoofed. Better is to use the mime type, e.g. “`image/jpeg`” as a selection criteria.

> How is the path to be formed? Absolute? Relative to what?

To make a script call as flexible as possible for as many users as possible, I prefer to filter files within the called batch script.

> Is it possible to use a wildcard?

see above, use the mime type as a criteria.

> Is there a logfile?  
> There is no dedicated log file for this app, except the Nextcloud log file. If you call a batch script you can add the following two lines of code to it to trace the command execution within the script:

```auto
#!/bin/sh

exec 2>/tmp/workflow_script-trace-$$.txt
set -x
...

```

Finally keep in mind that “%n” only contains the relative file path from the Nextcloud data directory, e.g. “/user/files/YourDirectory/20191111\_184052.jpg”. You need to prefix it with the data directory path to get a full qualified path 😉

---

<div class="post-metadata">

**Author:** ![BruderB](https://help.nextcloud.com/letter_avatar/bruderb/32/6_22734026db12803ebb3e059622ac3534.png) [@BruderB](https://help.nextcloud.com/u/BruderB)\
**Post date:** [October 24, 2020, 7:41pm UTC](https://help.nextcloud.com/t/need-help-with-workflow-script/95750/3 "2020-10-24T19:41:48Z")

</div>

Hej j-ed,

thank you very much for your hints.  
Yes, in fact I tried to search the forum and found that one which describes quite the same like I am looking for:  
[https://help.nextcloud.com/t/imagemagick-or-similar-convert-command-on-file-upload-flow/](https://help.nextcloud.com/t/imagemagick-or-similar-convert-command-on-file-upload-flow/)

But the OP already solved the fist step I am still hanging with.

So I followed your description and placed a script with the mentioned content in /var/www/nextcloud/onjpgexec.sh, made it be owned by www-data and chmodded it executable.  
Executing it from bash it creates a tracefile in /tmp.  
So far s good.

Also, I formed the condition for the execution as  
If “file created”  
and “file mime type” is “pictures”

In the run script field I wrote the whole path  
/var/www/nextcloud/onjpgexec.sh %n

Uploading a JPG to my files doesn’t make the tracefile being created.

What’s wrong?

---

<div class="post-metadata">

**Author:** ![j-ed](https://help.nextcloud.com/user_avatar/help.nextcloud.com/j-ed/32/7281_2.png) [@j-ed](https://help.nextcloud.com/u/j-ed)\
**Post date:** [October 25, 2020, 11:05am UTC](https://help.nextcloud.com/t/need-help-with-workflow-script/95750/4 "2020-10-25T11:05:55Z")

</div>

> [@BruderB](#):
>
> In the run script field I wrote the whole path  
> /var/www/nextcloud/onjpgexec.sh %n
> 
> Uploading a JPG to my files doesn’t make the tracefile being created.
> 
> What’s wrong?

1. I would recommend to quote the option **`%n`** → **`"%n"`** to make sure that possible spaces in file and path names are not split into several parts.

2. I don’t know any mime type which is called “picture” or do you meant "`image/jpeg`”?

3. I would check the access rights and ownership of the script file. I need to be accessible and executable by the web server user. Very often users are testing their scripts under a root environment where access to nearly everything is possible. The Nextcloud log file should contain further information about the reason why e.g. a script cannot be executed.

4. I personally prefer to save script files in a directory outside of the Nextcloud document root directory, e.g. in `/var/nextcloud/.scripts` etc., because the Nextcloud system check most likely will throw out an error if the unknown file is found. But this can be changed once the script execution problems have been solved.

---

<div class="post-metadata">

**Author:** ![BruderB](https://help.nextcloud.com/letter_avatar/bruderb/32/6_22734026db12803ebb3e059622ac3534.png) [@BruderB](https://help.nextcloud.com/u/BruderB)\
**Post date:** [October 25, 2020, 3:19pm UTC](https://help.nextcloud.com/t/need-help-with-workflow-script/95750/5 "2020-10-25T15:19:35Z")

</div>

Hej j-ed,

thanks again for caring!

1. Done. I put quotation marks in front of behind %n. On the other hand my test-JPGs are very easy-named: Just as they come from my cam, for example: 20201003\_144613.jpg

2. Well, of course… I’m using the German translation and in fact it’s called “Bilder”

 ![Bildschirmfoto von 2020-10-25 15-51-07](https://help.nextcloud.com/uploads/default/original/3X/d/f/df9426ab982c16a634f92643b8d40b3c61d3091a.png)

I also tried the “Benutzerdefinierter MIME-Typ” and put in “image/jpeg” - with no success.

1. I followed this hint, too. Have a look at the screenshot provided.

2. And yes: The directory .scripts and all its content is owned by www-data:www-data and the permissions on the script are set to 755.

Actually, I’m afraid to make a general mistake:  
Me, being user _and_ administrator on my NC have two menu-topics “Ablauf” in the tree on the left: One under ‘personal’ and the other under ‘Verwaltung’ - please see second screenshot. I am using the one under ‘Verwaltung’ because the other doesn’t show the option ‘run script’.

 ![Bildschirmfoto von 2020-10-25 15-53-12](https://help.nextcloud.com/uploads/default/original/3X/c/a/cae9c3855a97b905c1ed36d04ab652dd326c06f5.png)

Thanks,

Boris

---

<div class="post-metadata">

**Author:** ![j-ed](https://help.nextcloud.com/user_avatar/help.nextcloud.com/j-ed/32/7281_2.png) [@j-ed](https://help.nextcloud.com/u/j-ed)\
**Post date:** [October 25, 2020, 3:57pm UTC](https://help.nextcloud.com/t/need-help-with-workflow-script/95750/6 "2020-10-25T15:57:12Z")

</div>

That looks fine to me.

Have you added at least one more command to your batch file, beside the “exec” and “set” commands, e.g. **`echo "%1"`** to produce traceable output?

Are there any related messages in the Nextcloud log file?

---

<div class="post-metadata">

**Author:** ![BruderB](https://help.nextcloud.com/letter_avatar/bruderb/32/6_22734026db12803ebb3e059622ac3534.png) [@BruderB](https://help.nextcloud.com/u/BruderB)\
**Post date:** [October 25, 2020, 6:00pm UTC](https://help.nextcloud.com/t/need-help-with-workflow-script/95750/7 "2020-10-25T18:00:23Z")

</div>

Hej j-ed,

Thanks so much. You helped me keeping on topic and believing it will work…

Finally I found out the trigger condition is checked every five minutes by cronjob. So my trying was too impatient! I thought the trigger is fired right after uploading the JPG.

OK, it’s done - so far…

Last question - and I admit not having searched the forum:  
Now when the conversion is done, the script created a new file in the filesystem that has to be ‘registered’ within NC. How can that be done?

Thanks again,

Boris

---

<div class="post-metadata">

**Author:** ![j-ed](https://help.nextcloud.com/user_avatar/help.nextcloud.com/j-ed/32/7281_2.png) [@j-ed](https://help.nextcloud.com/u/j-ed)\
**Post date:** [October 26, 2020, 8:25am UTC](https://help.nextcloud.com/t/need-help-with-workflow-script/95750/8 "2020-10-26T08:25:36Z")

</div>

> [@BruderB](#):
>
> Now when the conversion is done, the script created a new file in the filesystem that has to be ‘registered’ within NC. How can that be done?

What about these lines of code:

```auto
if [-x ${docroot}/occ]
then
    # force rescan of new mp4-file
    ${docroot}/occ files:scan --no-ansi --quiet --shallow --unscanned --path="${full-path-to-new-file}" 2>/dev/null
else
    # occ command not found or not executable
    echo "Warning: '${docroot}/occ' cannot be executed!"
fi

```

BTW, it might also be a good idea to automatically remove the old file from your system 😉

---

<div class="post-metadata">

**Author:** ![BruderB](https://help.nextcloud.com/letter_avatar/bruderb/32/6_22734026db12803ebb3e059622ac3534.png) [@BruderB](https://help.nextcloud.com/u/BruderB)\
**Post date:** [October 27, 2020, 9:00pm UTC](https://help.nextcloud.com/t/need-help-with-workflow-script/95750/9 "2020-10-27T21:00:35Z")

</div>

OK j-ed,

Thank you very much! I am sure I will be able to bring this together and make my idea work just fine!  
Let’s close this topic and hope others will find it when working on a similar thing.

Thanks and regards,

Boris

---

<div class="post-metadata">

**Author:** ![BruderB](https://help.nextcloud.com/letter_avatar/bruderb/32/6_22734026db12803ebb3e059622ac3534.png) [@BruderB](https://help.nextcloud.com/u/BruderB)\
**Post date:** [November 10, 2020, 1:09pm UTC](https://help.nextcloud.com/t/need-help-with-workflow-script/95750/10 "2020-11-10T13:09:41Z")

</div>

Although I already marked the thread as closed (I just removed this flag), two questions came up while I was realizing the solution. So far I have achieved that images stored in NC are converted in a subdirectory.

Both questions have to do with saving computing resources:

1. i would like to limit the trigger for the script to a certain directory, otherwise the script runs every time an image is uploaded. The script could test itself in which directory it works, but then it has already been started unnecessarily.  
How do I have to formulate a second rule that describes the directory?

2. j-ed had provided a few lines of code as a template for registering the processed images in NC.  
I have not yet realized this part. But does it make sense in principle to realize this process for every file? I know the directory and the name of the file and it seems not necessary to _scan_ it. Is there any way I can register the file directly?

Thanks and greetings

Boris

---

<div class="post-metadata">

**Author:** ![j-ed](https://help.nextcloud.com/user_avatar/help.nextcloud.com/j-ed/32/7281_2.png) [@j-ed](https://help.nextcloud.com/u/j-ed)\
**Post date:** [November 10, 2020, 1:54pm UTC](https://help.nextcloud.com/t/need-help-with-workflow-script/95750/11 "2020-11-10T13:54:27Z")

</div>

> [@BruderB](#):
>
> i would like to limit the trigger for the script to a certain directory, otherwise the script runs every time an image is uploaded. The script could test itself in which directory it works, but then it has already been started unnecessarily.  
> How do I have to formulate a second rule that describes the directory?

I haven’t had the requirement to restrict the access to a specific directory yet, but due to the fact that no directory/path selector exists I would recommend to give the “file-name eqals regular-expression” a try to catch a specific directory.

> [@BruderB](#):
>
> j-ed had provided a few lines of code as a template for registering the processed images in NC.
> 
> I have not yet realized this part. But does it make sense in principle to realize this process for every file? I know the directory and the name of the file and it seems not necessary to _scan_ it. Is there any way I can register the file directly?

You register the file directly by executing the “occ files:scan …” command right after you’ve modified it. By providing the full path to the file, including the file name, only this item will be scanned and not the whole directory.

---

<div class="post-metadata">

**Author:** ![BruderB](https://help.nextcloud.com/letter_avatar/bruderb/32/6_22734026db12803ebb3e059622ac3534.png) [@BruderB](https://help.nextcloud.com/u/BruderB)\
**Post date:** [December 5, 2020, 4:36pm UTC](https://help.nextcloud.com/t/need-help-with-workflow-script/95750/12 "2020-12-05T16:36:46Z")

</div>

OK, it’s done. For some reason I had to change --unscanned to --all .  
Thank you very much again.

---

<div class="post-metadata">

**Author:** ![wwe](https://help.nextcloud.com/user_avatar/help.nextcloud.com/wwe/32/72963_2.png) [@wwe](https://help.nextcloud.com/u/wwe)\
**Post date:** [December 12, 2024, 7:11pm UTC](https://help.nextcloud.com/t/need-help-with-workflow-script/95750/13 "2024-12-12T19:11:09Z")

</div>



---

<div class="post-metadata">

**Author:** ![wwe](https://help.nextcloud.com/user_avatar/help.nextcloud.com/wwe/32/72963_2.png) [@wwe](https://help.nextcloud.com/u/wwe)\
**Post date:** [December 12, 2024, 7:11pm UTC](https://help.nextcloud.com/t/need-help-with-workflow-script/95750/14 "2024-12-12T19:11:11Z")

</div>


