Download youtube video automation

My typo: it has to be })()
BTW: A script than can’t be saved in the Editor won’t run anyway. You can also click on the hammer in the upper right to have the Editor syntax check the code.

thank you that fixed it.
However I can still not run it via “apply rule” in Devonthink since I still get:

on performsmartrule (Error:Error:Message not understood).

But when now add another logmessage after the foreach and I run it via the Script Editor I can see the log in the Devonthink log.

It looks like the records parameter is empty when I apply the rule. However the smartrule shows me records in devonthink. My understanding was that records contains all records of the smart rule.

More info on that here:

are you sure that the selected records have a plainText at all? What happens if you do
app.logMessage(r.plainText() || "NO TEXT THERE") or app.logMessage(r.uuid())?

1 Like

I really don’t get it… this is the complete script:

function performsmartrule(records) {
let app=Application(“DEVONthink 3”);
app.logMessage(“hello world”);

}

(() => {
const app = Application(“DEVONthink 3”);
performsmartrule(app.selectedRecords());
})()

When I run it in the script editor it prints: “Hello World” in the log.
But when I run it in the smart rule I get:

on performSmartRule (Error:Error:Message not understood).
This is how I setup the smart rule and I hit “apply rule” to run the script.

This drives me nuts…

1 Like

ok I got the problem, it looks like the issue was the missing .scpt. now I need to figure out how to get the records from the smart rule since the records is always undefined even though there are records shown in devonthink when I select the smart rule.
However it works when I actually select some of those records and run the script in the editor.
Selecting the records and run the rule does not work. No log is shown and I get the (Error:Error:no Error)

Smart Rules don’t act on selectedRecords. I don’t know JavaScript but you’ll probably have to change this line

performsmartrule(app.selectedRecords());

well for some reason I always got no logs so I restarted Devonthink… and now it works… jesus…

So I was fiddling around with it a bit and I don’t think it is a good idea to use javascript for devonthink scripting at least not in the smart rules.
It seems not very consistent to me. For example I can reproduce this behavior everytime:

records.forEach(r => app.logMessage("foo"));
→ When I select all 3 records of the smart rule in DT and run the script via script editor I get in the log:
foo
foo
foo
→ when I run this in DT I get the same log output + and ending (Error:Error:no Error)

then I change it to:
records.forEach(r => app.logMessage(r.name()));
→ same procedure via Script Editor I get all 3 names
→ in DT I only get (Error:Error:no Error)
→ When I restart DT and apply the rule again I get all 3 names followed by (Error:Error:no Error)

now I change it to:
records.forEach(r => app.logMessage(r.url()));
→ same procedure via Script Editor everything works
→ in DT I only get (Error:Error:no Error) but now, no matter how often I restart I can not get the url…

this is really frustration

1 Like

That’s of course correct. The callisthenics in the code are just there so that one is able to run the code with some selected records in the script editor.

I agree. I tried it again, and the whole thing smells extremely fishy. I manage to get a meaningful
app.logMessage(records.length.toString())
so the JS function does get an object that has a length property. But apart from that, no luck: iterating over the records just does nothing, it seems (tried for i = 0..., for (r in records) and records.forEach() so far).
In my opinion, DT’s inner workings leave something to be desired here. Given that JavaScript code outside of a smart rule works ok generally, I suppose that these problems result from the implementation of the interface to the scripting language in smart rules. Seeing that Hazel has no problems in doing exactly the same thing here, it seems to be possible to support both languages for external scripts.

Given that JS is wastly more popular then AppleScript nowadays and it is the only way to have automation on i*OS, I hope that this will be fixed.

1 Like

The cool kids use AppleScript :sunglasses:

It’s also widely known that AppleScripters are in general happier, prettier and better smelling than any other script language users. In a perfect world there would only be one language: AppleScript(Objective-C).

2 Likes

Also, since AppleScript is so verbose (bordering on logorrhea), it is good for storage sellers. A whole bunch of win-win-win points.
Unfortunately, all these advantages are apparently lost on many people: AS is not even one of the fifty most used languages (according to index | TIOBE - The Software Quality Company). A lot of convincing to do, it seems.

2 Likes

I really don’t know what you mean

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

set theRandomLanguage to "JavaScript"
length of theRandomLanguage
--> 10
--> Too concise

set theOneAndOnlyLanguage to ((current application's NSString's stringWithString:theRandomLanguage)'s stringByReplacingOccurrencesOfString:"Java" withString:"Apple" options:(current application's NSRegularExpressionSearch) range:{location:0, |length|:length of theRandomLanguage}) as string
length of theOneAndOnlyLanguage
--> 11
--> Just right

3 Likes

So I tried my luck with AppleScript but I am stuck now. For some reason I do not get the complete command concatenated.

So this is the command which works by executing it via terminal:

/usr/local/Cellar/youtube-dl/2021.3.3/bin/youtube-dl -o '~/Library/Application Support/DEVONthink 3/Inbox/%(title)s.%(ext)s' https://www.youtube.com/watch\?v\=__cjZ43g7-0

However in my script I try this:

 repeat with thisRecord in theRecords
 			set thisName to URL of thisRecord
 			set params to " -o "
 			set theInbox to "'~/Library/Application Support/DEVONthink 3/Inbox/%(title)s.%(ext)s' "
 			log message thisName
 			do shell script "/usr/local/Cellar/youtube-dl/2021.3.3/bin/youtube-dl" & quoted form of params & quoted form of theInbox & quoted form of thisName
end repeat

And I get this error:
sh: /usr/local/Cellar/youtube-dl/2021.3.3/bin/youtube-dl -o '~/Library/Application Support/DEVONthink 3/Inbox/%(title)s.%(ext)s' https://www.youtube.com/watch?v=__cjZ43g7-0: No such file or directory

which looks exactly what I want right?

1 Like

Try to omit the ~ and use the full path.

I’m stumped.

My next step would be to try running the following from script editor:

set thisName to “https://www.youtube.com/watch?v=__cjZ43g7-0”
set params to " -o "
set theInbox to "'~/Library/Application Support/DEVONthink 3/Inbox/%(title)s.%(ext)s' "
do shell script "/usr/local/Cellar/youtube-dl/2021.3.3/bin/youtube-dl" & quoted form of params & quoted form of theInbox & quoted form of thisName

This is a complete stab in the dark, it just eliminates one variable (DT/smart rules) from the equation. If it still doesn’t work, I think I’d try putting everything together as a single variable, so

set theCommand to "/usr/local/Cellar/youtube-dl/2021.3.3/bin/youtube-dl" & params & theInbox & thisName
do shell script quoted form of theCommand

Again, I don’t actually know what I’m doing - those would just be my next steps. (And I see @pete31 has also posted; he does know what he’s doing.)

1 Like

@KaputtZimon and @Blanc That’s what I always do while writing a do shell script:

  • Put everything in a variable
  • Check output
  • Do a test run
  • Afterwards use the command directly without variable

Note: Code below is not working, it’s just an example how a do shell script can be easier debugged.

set theShellScript to "/usr/local/Cellar/youtube-dl/2021.3.3/bin/youtube-dl" & quoted form of params & quoted form of theInbox & quoted form of thisName
#do shell script theShellScript
1 Like

@KaputtZimon didn’t check your command further but you e.g. put -o in straight quotes by using quoted form of - that’s probably not what you want as you’re not doing that in the command that works in Terminal.

That was my idea too, which is why I removed the “quoted form of” for the individual sections in the second half of this post.

1 Like

Yeah saw that later, sorry.

1 Like