Monday, July 26, 2021

#1 2021-07-20 11:30:23 pm

Sepa
Member
Registered: 2020-10-09
Posts: 32

Clipboard text formating

The AppleScript creates a set of folders at the folder path stored clipboard path.
This works well when the path is copied using the contextual finder menu Copy as Pathname.

On the other hand, the AppleScript fails when the clipboard path originates with the Adobe Bridge Application Copy path function.

The following manual workaround does work with the Adobe Bridge and might hint at a solution
1. Copy the folder path using the Adobe Bridge Application
2. Paste the path into a text file
3. Reselect the path
4. Copy to clipboard
5. Execute AppleScript

Something happens in this workaround that validates the clipboard text for the AppleScript.

Can an AppleScript take the text that is the clipboard and reformat it to the default clipboard text? The idea here is to strip off any text formatting introduced by the Bridge application.

Here is the line from the Bridge Application script that copies the path to the clipboard.

app.system('echo '+ thumbs[0].spec.fsName + '|pbcopy');

Last edited by Sepa (2021-07-21 12:10:15 am)


Filed under: clipboard, applescript

Offline

 

#2 2021-07-21 07:34:22 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1954

Re: Clipboard text formating

What AppleScript, what workaround... Why do not simply

Applescript:


set thePath to the clipboard as string

Assuming the clipboard contains Posix path in plain text form or RTF form, you can create folder this way:

Applescript:


set posixPath to the clipboard as string
set hfsPath to posixPath as POSIX file as text

tell application "Finder"
   make new folder at folder hfsPath with properties {name:"myNewFolder"}
end tell

Last edited by KniazidisR (2021-07-21 07:46:53 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#3 2021-07-21 06:06:48 pm

Sepa
Member
Registered: 2020-10-09
Posts: 32

Re: Clipboard text formating

Thanks, your solution works when the clipboard path originates with the right-click contextual menu. When the path originates with the Bridge app the apple script produces the following"

error "Finder got an error: Can’t get folder \"Macintosh HD:Users:Shared:WEB
\"." number -1728 from folder "Macintosh HD:Users:Shared:WEB
"

Probably the path created with the Bridge app is not a Posix path in plain-text form or RTF path. Honestly, I am not sure what type of path is created when using the Adobe Bridge app to copy to the clipboard.
The path from the Bridge app looks like a conventional path when copied to a txt file.
For example: /Users/Shared/WEB
Can AppleScript check the type of path that is in the clipboard?

Also, does this workaround make sense?
1. Create a temporary txt file
2. Paste the path from the Bridge app.
3. Reselect the path in the temporary file,
4. Copy the path to the clipboard,
5. Remove the temporary file,
6. Run the apple script to create the folders

Offline

 

#4 2021-07-22 01:47:25 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6677

Re: Clipboard text formating

This will get the POSIX path of a single file (or the first, if more than one is selected in Bridge):

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
set urlString to pb's stringForType:"public.file-url"
set posixPath to (current application's NSURL's URLWithString:urlString)'s |path|() as text

To get a list of paths, you can use this:

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
set theData to pb's dataForType:(current application's NSFilenamesPboardType)
set thePaths to (current application's NSPropertyListSerialization's propertyListWithData:theData options:0 format:(missing value) |error|:(missing value)) as list


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2021-07-22 04:24:33 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 854

Re: Clipboard text formating

You could use Shanes code or you could get the right type from clipboard to get public.file-url
in clipboard we do that with as «class furl»

by doing this...

Applescript:

-- Give you a POSIX path of file path.
set theClipboard to POSIX path of (the clipboard as «class furl»)
-- To check what clipboard store you could use clipboard info.
return clipboard info


if you are the expert, who will you call if its not your imagination.

Offline

 

#6 2021-07-22 11:25:43 pm

Sepa
Member
Registered: 2020-10-09
Posts: 32

Re: Clipboard text formating

Thank you for providing the code. I run a test and found a difference between the Bridge clipboard and the FInder clipboard.

The path copied into the clipboard in plain text:
/Users/Shared/WEB

The path copied into the clipboard using the FInder and run through the Applescript:
{{«class utf8», 17}, {«class ut16», 36}, {string, 17}, {Unicode text, 34}}

The path copied into the clipboard using the Bridge app and run through Applescript:
{{«class utf8», 18}, {«class ut16», 38}, {string, 18}, {Unicode text, 36}}


The format looks consistent, the digits change in the clipboard depending on the method used to copy the path. What does the difference in numbers mean?

Offline

 

#7 2021-07-23 12:54:57 am

Sepa
Member
Registered: 2020-10-09
Posts: 32

Re: Clipboard text formating

According to this online tool https://www.soscisurvey.de/tools/view-chars.php that detects hidden characters in copy and pate operations:

The path copied from the Finder has 17 characters, 17 bytes

The path copied using the Bridge app has 19 characters, 19 bytes

Any ideas to manage the extra characters?

hidden characters

hidden characters

Last edited by Sepa (2021-07-23 01:03:50 am)

Offline

 

#8 2021-07-23 01:45:08 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 854

Re: Clipboard text formating

It looks to me you are using pbcopy to copy a file reference to clipboard.
By doing that you will not get public-file-url but instead public-utf8-plain-text

When I select a file in Finder on my Mojave and use keyboard shortcut cmd + c
I get public-file-url.

To use pbcopy I made this

Applescript:

tell application "Finder" to set theSelection to POSIX path of (its selection as text)
do shell script "echo " & theSelection & " | pbcopy"
clipboard info

return the clipboard as «class utf8»


if you are the expert, who will you call if its not your imagination.

Offline

 

#9 2021-07-23 12:50:40 pm

Sepa
Member
Registered: 2020-10-09
Posts: 32

Re: Clipboard text formating

I am trying with two different functions in the Bridge app to copy to the clipboard.
Not sure which function is technically correct.

I am still lost in understanding why I am unable to execute the folder-setup.scpt when the clipboard path input is done with the Bridge app. On the other hand, when the folder path is done with the Finder the script executes correctly.

function pathToClipboard(){
var sels = app.document.selections;
app.system("echo "+decodeURI(sels[0].spec.fsName) +"|clip");
}


function copyPath(){
var thumbs = app.document.selections;           
app.system('echo '+ thumbs[0].spec.fsName + '|pbcopy');
return;
}

Last edited by Sepa (2021-07-23 12:50:52 pm)

Offline

 

#10 2021-07-23 10:02:52 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1954

Re: Clipboard text formating

Sepa wrote:

According to this online tool https://www.soscisurvey.de/tools/view-chars.php that detects hidden characters in copy and pate operations:

The path copied from the Finder has 17 characters, 17 bytes

The path copied using the Bridge app has 19 characters, 19 bytes

Any ideas to manage the extra characters?

https://drive.google.com/file/d/1QRbkcY … sp=sharing

https://drive.google.com/file/d/14jrsB4 … sp=sharing


Was it really difficult to provide the strings (paths) that screenshots show instead of broken links to screenshots. Okay, I'll provide them for you:

Finder.app: "/Users/Shared/WEB"
Bridge.app: "/Users/Shared/WEB\r\n"

So, your workaround should be like this:

Applescript:


set posixPath to text 1 thru - 3 of (the clipboard as string)
set hfsPath to posixPath as POSIX file as text

tell application "Finder"
make new folder at folder hfsPath with properties {name:"myNewFolder"}
end tell

NOTE: Instead of using highly specialized third-party utilities, I recommend using the Script Debugger. It shows in the area of variables visible and invisible text characters. It should show the path from Bridge.app exactly as I provided it above. After executing set posixPath to the clipboard as string

Last edited by KniazidisR (2021-07-23 10:33:36 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#11 2021-07-24 02:28:57 pm

Sepa
Member
Registered: 2020-10-09
Posts: 32

Re: Clipboard text formating

Your workflow removed the obscure characters introduce by the Bridge app fixing the issue.
Now the Bridge copy to clipboard function works with the folder-setup script in the same way it does with the OSX Finder. That is awesome!

I try to include the screenshots to show the string paths with the hidden characters and without the hidden characters. I should have included a text version as a backup. Another lesson learned.

Many thanks for your help!

Last edited by Sepa (2021-07-24 02:29:25 pm)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)