Applescript to Bash

Been having trouble trying to debug my script.

Im trying to convert .webp files to png so I could actually see them in DT.

So far I’ve created a bash script that converts webp to png using dwebp. Works. Only issue is trying to debug what went wrong in the applescript process.

This is what I have so far

There is probably an easier way to do this or Im going about this wrong.

End goal is being able to view images through DTs webkit browser; safari. This was the easiest way of doing this without saving as HTML, as I hate saving as HTML because it’s not portable later.

Many of my files are memes and other images that are saved in image format that I bookmark and organize with DT. I use chrome so all my files are being saved as .webp but DTs internal browser safari cannot view them.

So far saving to a folder then converting using hazel then running the bash script to convert webp to png then dragging the PNG into DT, but thats such a hassle as hazel is such a process hog sometimes and I only have it running for this one process which Id rather just get rid of hazel all together.

So far I’ve created the bash file and sort of put together this applescript… Im not familiar at all with AS. shell script is +x as well.

– WEBP TO PNG

tell application id “DNtp”

try

set this_selection to the selection

if this_selection is {} then error “Please select some images.”

repeat with this_item in this_selection

try

do shell script "/Users/ron/Library/Application Scripts/com.devon-technologies.think3/Menu/Images/webp2png.sh " & this_selection

end try

end repeat

on error error_message number error_number

if the error_number is not -128 then display alert “DEVONthink” message error_message as warning

end try

end tell

And what exactly is the problem with this script? Any error message?

It does nothing. No error message.

You’re feeding your bash script DEVONthink references. Does it know how to resolve those?

You also have a coding error in the script: the loop variable is this_item, but the shell script is given this_selection

1 Like

I doubt that.

@ron You probably want to pass a path to the shell script. Can’t test but this should work

-- WEBP TO PNG

tell application id "DNtp"
	try
		set this_selection to the selection
		if this_selection is {} then error "Please select some images."
		
		repeat with this_item in this_selection
			set thisPath to path of this_item
			try
				do shell script "/Users/ron/Library/Application Scripts/com.devon-technologies.think3/Menu/Images/webp2png.sh " & quoted form of thisPath
			end try
		end repeat
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell

Also you’ve used a repeat with “this_item”, but inside the repeat you’ve used “this_selection” instead of “this_item”. This does’t make sense, either you can pass a list of e.g. paths to a shell script or you need a repeat to pass e.g. each path on its own.

It won’t give you an error as you’ve wrapped the do shell script line in an try block. It’s a good idea to only use try blocks when a script already does what you want, otherwise you won’t know what’s the problem if it doesn’t work.

1 Like

Yes, of course.
I would also assume that his shell script doesn’t do much by way of error trapping, so it’ll fail when given a group by mistake.
He would have found out eventually, which in my mind is the best way to learning new tricks.

2 Likes

Is there anyway to debug Applescript? I want to see what is getting passed to the bash script so I can edit the script accordingly. Still blank here. Yeah totally new to AS. Thought “this_selection” was the selected selection file path in devonthink. Had just yanked a similar script from the script folder and thought it would atleast give an error msg so I could work my way around it but alas, not even a way to debug it that I’ve found so far. Is there anyway to see whats getting passed through bash or if echo in bash works?
Maybe >&2 echo $1 or similar?

edit: I can start with ‘display dialog’

I usually just use Script Debugger to see what’s going on.

You could add

return thisPath 

inside the repeat, this would end the script at this point and you could see in Script Editor what’s passed to your shell script.

If necessary I sometimes add e.g.

display dialog thisPath as string

but that’s only needed if a script doesn’t work in another app after it already worked fine in Script Debugger.

Or you could try this code by @BLUEFROG to log AppleScript.

But I think in your case that’s all unnecessary as there’s nothing that could go wrong with getting a record’s path from DEVONthink. Also thisPath is already correctly wrapped in quotes by the quoted form of, that’s all fine.

So I’m afraid it’s your shell script that doesn’t work. I’ve only little knowledge of shell scripts, but if you post it I’ll take a look.

Note: Bash and AppleScript are not the same thing.

Create a shell script

#!/usr/bin/osascript
on run (argv)
	set theInput to (item 1 of argv)
	tell application "SystemUIServer"
		activate
		display dialog theInput as string
	end tell
end run

and call it via

do shell script "/Users/Username/Desktop/displaydialog.sh " & quoted form of thisPath

I tossed the bash script and just used applescript.

So far, this uses the selected file webp and turns it into a .png using dwebp.

Does DT automatically index the file inside the database?
It doesn’t seem to refresh automatically and update the new file.

Now I need to be able:

  • some how make DT update itself with the new file inside the database
  • rm thisPath and presto

Was using ${1##.*}.png" in bash to swap the extension from .webp to .png but I’ll take webp.png :smile:


tell application id "DNtp"
	try
		set this_selection to the selection
		if this_selection is {} then error "Please select some images."
		
		repeat with this_item in this_selection
			set thisPath to path of this_item
			try

				do shell script "/usr/local/bin/dwebp -o " & thisPath & ".png " & thisPath
				
			end try
		end repeat
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
	end try
end tell```

Please edit your post and wrap the script in three backticks, like this

```
script

```

Note: You have to paste it again as plain text as the previous version without backticks is messed up.

Thanks.

1 Like

Ok I think I need to read more about

deconsolidate record

I think DT doesn’t update if a new file is created, so I need to put in a record.

edit: I see what went wrong. I can’t put a file I placed inside DTs database and index it into DT. I need to save the .png outside of the database, put it into the database so it will make it’s own file and catalog it. So right now I am:

  • Converting the .webp into .png
  • Placing the png file into the same folder inside DTs database as where webp is located. This is incorrect as it won’t be read by DT.

I need to:

  • Convert the .webp into .png
  • Place the .png OUTSIDE the DT database into a different folder, maybe ~ or something.
  • Index or add the png file and also copy the same attributes such as URL, alias, comments etc… into the same group

You should never add files into a database package like this. DEVONthink doesn’t know what you’ve done and if your script worked you have now created an orphan.

Please do File > Verify & Repair. You can find the orphans (if your script created any) afterwards in the global inbox.

If you want to use a CLI tool that creates new files you need to index a folder and use this as your output group.

Yes thank you. I just found that out, I edited the post above.