Hello. I was wondering whether there was a script or similar for adding a specific URL to the URL field of all items in a group?
There isn’t a specific script for it, but Script menu > Comments > Convert comments to URL could easily be adapted for such a thing. Note that script is recursive, so it would apply to items in all subgroups. This may or may not be a desirable thing. If you’d like to be more judicious in modifying files, it would be very easy to write a simple script to change the URL on selected files.
Thanks for the advice. I tried doing that. Because I’m an AppleScript novice, I decided to make things simpler by having the script prompt me for the desired URL and then assign it to all items in the selected group, rather than having to set the URL by getting it from somewhere automatically. I came up with the following. The script asks me for the input (twice for some reason), and doesn’t throw up any errors, but also doesn’t seem to be doing anything; i.e., even after running the script, the URL fields are all empty.
Can anyone offer some pointers to where I’m going wrong?
Thanks!
tell application id "com.devon-technologies.thinkpro2"
try
set this_selection to the selection
if this_selection is {} then error "Please select some contents."
show progress indicator "Updating URLs..."
my copyURL(this_selection)
hide progress indicator
on error error_message number error_number
hide progress indicator
if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
end try
end tell
on copyURL(theseRecords)
local this_record, my_URL, this_type
set my_URL to display dialog "What is the URL you want to use?" default answer ""
tell application id "com.devon-technologies.thinkpro2"
repeat with this_record in theseRecords
set this_type to type of this_record
if this_type is group then
step progress indicator (name of this_record as string)
my copyURL(children of this_record)
else
-- set this_URL to myURL
set the URL of this_record to my_URL
-- set the comment of this_record to ""
end if
end repeat
end tell
end copyURL
set my_URL to display dialog "What is the URL you want to use?" default answer ""
If you run this in Script Editor with Replies shown, you would see a list is returned. This list contains a text object and a button object. Those are important to understand, as they’re what you’d be requesting (depending on the situation).
*Note: This applies to any use of display dialog, not just within DEVONthink. display dialog is a Standard Addition provided by Apple, not us.
As per this screencap, you want the text returned of the dialog. Make sense?
Also, you have it requesting a URL in the handler which can be called repeatedly with multiple files or groups selected. While it’s not entirely optimal, moving it after this line ```
set this_selection to the selection
PS: Good on ya for digging into this. It was a good effort indeed! :smiley:
Thanks very much for the guidance and encouragement. I think I followed your advice, but I get the error message: “The variable my_URL is not defined.”
I’ll reread your message tomorrow and try fiddling around with the order of the commands.
Ahh… sorry about that. I wasn’t running your code, just glancing at it with a little lesson in mind…
Change ```
my copyURL(this_selection)
to
my copyURL(this_selection, my_URL)
and change ```
on copyURL(theseRecords)
``` to ```
on copyURL(theseRecords, my_URL)
Remember that computers are incredibly fast but very stupid. (You gotta tell them exactly what to do! )
My overlooked details in my little “lesson”…
[]The URL variable wasn’t passed when the handler was called. It was only passing the selection.
[]The handler only had one parameter, for the selected records, specified. This would have errored as well unless you added a parameter for the URL as well (as shown above).
Cheers!
Thanks again. I’m afraid I’m still a bit lost, though. The error message I get now is:
when I select the group containing the items whose URL fields I want to fill.
When I select directly the actual items whose fields I want to change, I don’t get an error message, but the script has no effect.
It seemed to me that the problem must reside in the routine for cycling through the records in a group, so I decided to get rid of that part and just select all the records that I want to change directly. The script is now much shorter and simpler but still doesn’t have any effect. If you have time, any further pointers would be wonderful.
tell application id "com.devon-technologies.thinkpro2"
try
set this_selection to the selection
set my_URL to display dialog "What is the URL you want to use?" default answer "" default button 1
if this_selection is {} then error "Please select some contents."
show progress indicator "Updating URLs..."
my copyURL(this_selection, my_URL)
hide progress indicator
on error error_message number error_number
hide progress indicator
if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
end try
end tell
on copyURL(theseRecords, my_URL)
local this_record, my_URL
tell application id "com.devon-technologies.thinkpro2"
set the URL of this_record to my_URL
end tell
end copyURL
This is another lesson in modifying someone else’s code
Here’s my take on the problem, in a fairly simple form…
property htmlPrefixes : {"http", "https", "x"}
tell application id "DNtp"
repeat with thisRecord in (selection as list)
set my_URL to (text returned of (display dialog "What is the URL you want to use?" default answer ""))
if (my_URL ≠ "") and ((word 1 of my_URL) is in htmlPrefixes) then
set URL of thisRecord to my_URL
else
display alert "This does not appear to be a valid URL."
end if
end repeat
end tell
Note: This is not doing a full URL validation. It’s just checking for the string beginning with common URL methods. The “x” supports using an x-devonthink-item URL pasted in.
You’ve got a curly quote instead of a straight quote in the – default answer (i.e fourth line of code)-- line
Ugh. It’s from pasting into the forum. Sigh.
- Corrected
Thanks very much for the continuing help. That was much simpler for me to follow. The script as you had it asked me for a URL for each item separately, so I switched the order of the first and second lines after the “Tell” line. That did the trick. Thanks once again.
A quick question about comments:
I adapted the above script to do the same thing for “the comment” of all selected records. At first I thought I had done something wrong, because nothing shows up in the Comment field in column view. But Show Info reveals the supplied comment in “Spotlight Comments”. Is the Comment field something different, and are its contents only visible in column view?
Note that this isn’t a problem: I’ve achieved what I wanted to. It just made me wonder what the difference is.
Glad to be of help.
Yes, Comments are RTF(D) / PDF specific metadata. Spotlight Comments are usable and editable on all records in a database.