Copying pictures

About SheepShaver, a PPC Mac emulator for Windows, MacOS X, and Linux that can run System 7.5.3 to MacOS 9.0.4.

Moderators: Cat_7, Ronald P. Regensburg, ClockWise

emendelson
Forum All-Star
Posts: 1706
Joined: Tue Oct 14, 2008 12:12 am

Re: Copying pictures

Post by emendelson »

This is the same thing that I've been working on. As you say, the viewing routine has to come before the file gets deleted.

I've been banging my forehead on the desk trying to figure out how to make the viewing window timeout after n seconds if the user doesn't press a key. This is the script fragment that I've been using:

Code: Select all

if showImage is true then
	set tempFile to "~/Documents/clipboardConvFile." & destExtension
		try
			do shell script "qlmanage -p" & space & tempFile & " & QL_PID=$! && read -sn1 && kill $QL_PID"
		end try
end if
From a command line or a bash script, the "read" command can include the parameter -t 10 (or -sn1t10) to add a timeout of 10 seconds, but the -t switch has no effect when run from an AppleScript. The -t switch works when you include the command in a bash script - but when I tried to launch the bash script from the AppleScript, it didn't display the qlmanage window. I can't figure out why, but someone more expert than I am can probably figure it out.

That's as far as I've got today. I think the only thing missing is a reliable way to timeout the preview.
User avatar
mabam
Master Emulator
Posts: 497
Joined: Wed Apr 10, 2013 9:32 am

Re: Copying pictures

Post by mabam »

I’ve found a solution on stackoverflow.com: Add “ > /dev/null 2>&1 & echo $!” to the shell command.
This also returns the PID of the process which we don’t need when using its name to quit it from AppleScript. So the following should work:

Code: Select all

do shell script "/usr/bin/qlmanage -p ~/Documents/clipboardConvFile." & destExtension & " > /dev/null 2>&1 &"
delay 5
try
	tell application "qlmanage" to quit
end try

emendelson wrote: Sun Aug 29, 2021 1:57 pm As you say, the viewing routine has to come before the file gets deleted.
I said we would have to do the moving of the image to the trash first and preview it from there. But that was error in my logic (complicated reasoning on my side …).

You’re completely right with your statement: The viewing routing has to come before deletion of the files, so before the very last try block in the script.
Last edited by mabam on Sun Aug 29, 2021 8:31 pm, edited 1 time in total.
emendelson
Forum All-Star
Posts: 1706
Joined: Tue Oct 14, 2008 12:12 am

Re: Copying pictures

Post by emendelson »

This is very similar to what I've been trying, but I've been trying to add something (immediately after the line that runs qlmanage) which tests whether qlmanage is still running before giving the quit command. Instead of delay 5, I tried various forms of repeat loop that tested whether the pid or the process existed; if the pid or process didn't exist, I exited the repeat; if it did exist, the repeat loop would delay 1 until it repeated 5 times, and then give the quit command. But I could never make this work correctly. Clearly I don't know enough AppleScript to do this, though I searched through various solutions online.

Do you think this sounds possible?
User avatar
mabam
Master Emulator
Posts: 497
Joined: Wed Apr 10, 2013 9:32 am

Re: Copying pictures

Post by mabam »

I have found two solutions.

The first one is a shell one liner found on https://www.cyberciti.biz/faq/linux-run ... ime-limit/:

Code: Select all

do shell script "doalarm() { perl -e 'alarm shift; exec @ARGV' -- \"$@\"; }; doalarm 5 /usr/bin/qlmanage -p ~/Documents/clipboardConvFile." & destExtension & " > /dev/null 2>&1 || exit 0"

The second one handles the time limit completely in AppleScript:

Code: Select all

do shell script "/usr/bin/qlmanage -p ~/Documents/clipboardConvFile." & destExtension & " > /dev/null 2>&1 &"

set counter to 0
repeat until counter is 5
	delay 0.25
	set counter to counter + 0.25
	tell application "System Events"
		if (name of every process) does not contain "qlmanage" then
			exit repeat
		end if
	end tell
end repeat
if counter is 5 then
	tell application "qlmanage" to quit
end if

Though the first one is shorter, it involves calling an extra binary (perl) which seems to force quit qlmanage if it still runs after 5 sec, resulting in an error message I had to make shut up by adding “|| exit 0”.

The second one quits qlmanage nicely.

(I had also found do shell script "/usr/bin/qlmanage -p ~/Documents/clipboardConvFile." & destExtension & " & read -t 5 || kill $!" which is much easier. But it takes the full 5 seconds even if the user closes the previewed image earlier. So nice to know for other purposes, but not suitable for our script.)
emendelson
Forum All-Star
Posts: 1706
Joined: Tue Oct 14, 2008 12:12 am

Re: Copying pictures

Post by emendelson »

That long block of script seems to work perfectly, and is exactly what I was trying to figure out, but couldn't. Again: well done!

I've uploaded a notarized app to the same address linked in a previous post. The new version shows the preview if (1) the word Show is in the application name or (2) the word Key or the word Option is in the application name AND the user holds down the option key when launching the application. I haven't thoroughly tested (2) but it seemed to work when I tried it briefly.

I've now also added this to my Mac OS 9 application for macOS. In that application, you go to the Apple Menu and choose Clipboard Image to Host, and the application sends a signal to the host to run the clipboard converter app (with no option to show the converted image, though I'll probably figure out a way to add that option later).

Again, thank you for your terrific work on solving this problem!
User avatar
Ronald P. Regensburg
Expert User
Posts: 7821
Joined: Thu Feb 09, 2006 10:24 pm
Location: Amsterdam, Netherlands

Re: Copying pictures

Post by Ronald P. Regensburg »

emendelson wrote: Sun Aug 29, 2021 11:36 pmI've uploaded a notarized app to the same address linked in a previous post.
I needed to search the topic to find the link. Better to post it again: http://www.columbia.edu/~em36/ConvertClipboardPICT.zip
User avatar
mabam
Master Emulator
Posts: 497
Joined: Wed Apr 10, 2013 9:32 am

Re: Copying pictures

Post by mabam »

Thank you for the new version!

The conversion still works fine but I can’t make the preview work by renaming the app. Not permanently (“Show”) and not via the option key (“Key”/“Option”).
It does work if changing “property showImage : false” to “true” in the script, though.

Some years ago I had searched for a possibility to change behaviour of an AppleScript app when launched with the option key pressed. That was another thing where I only found the answer: “Not possible”.
You proved the opposite. Now I know I can find that required piece of code in your app. :-)
User avatar
Ronald P. Regensburg
Expert User
Posts: 7821
Joined: Thu Feb 09, 2006 10:24 pm
Location: Amsterdam, Netherlands

Re: Copying pictures

Post by Ronald P. Regensburg »

Where and how am I supposed to see the preview?

In the script I changed

Code: Select all

property showImage : false
to

Code: Select all

property showImage : true
but I see no preview.
User avatar
mabam
Master Emulator
Posts: 497
Joined: Wed Apr 10, 2013 9:32 am

Re: Copying pictures

Post by mabam »

That’s exactly what I did. Then, after copying a PICT image from SheepShaver, I ran the app and the preview was displayed.
emendelson
Forum All-Star
Posts: 1706
Joined: Tue Oct 14, 2008 12:12 am

Re: Copying pictures

Post by emendelson »

Could you try it one more time? Sometimes AppleScripts don't work correctly the first time you run them.

If you still don't see the preview, try changing the name of the app to "ConvertClipboardPICT Show" or anything else with "show" in the name.

Also, it's possible that the preview will be hidden behind another window. Please try to move all windows away from the upper center of your screen before you run the script.

If nothing works, then it's possible that some odd permission error is getting in the way. I'll have to look into it further.
User avatar
Ronald P. Regensburg
Expert User
Posts: 7821
Joined: Thu Feb 09, 2006 10:24 pm
Location: Amsterdam, Netherlands

Re: Copying pictures

Post by Ronald P. Regensburg »

Closed all windows first. No preview.
Changed the name of the script app to "ConvertClipboardPICT Show". Still no preview.

(The conversion itself worked fine both times.)
emendelson
Forum All-Star
Posts: 1706
Joined: Tue Oct 14, 2008 12:12 am

Re: Copying pictures

Post by emendelson »

Possibly a permissions error. Try this. Open a terminal, enter

Code: Select all

xattr -rc 
with a space at the end. Don't forget the space. Drag the application into the terminal window and press Return. Then try again.

If that doesn't work, I'll experiment with a different user account to see if there's a way around the problem, but that will have to wait for a few days, as I'll be traveling all this week.
User avatar
Ronald P. Regensburg
Expert User
Posts: 7821
Joined: Thu Feb 09, 2006 10:24 pm
Location: Amsterdam, Netherlands

Re: Copying pictures

Post by Ronald P. Regensburg »

No change.
User avatar
mabam
Master Emulator
Posts: 497
Joined: Wed Apr 10, 2013 9:32 am

Re: Copying pictures

Post by mabam »

If you run

Code: Select all

do shell script "/usr/bin/qlmanage -p PATH_TO_SOME_IMAGE > /dev/null 2>&1 &"
in AppleScript (substitute PATH_TO_SOME_IMAGE), does that make a preview appear?
emendelson
Forum All-Star
Posts: 1706
Joined: Tue Oct 14, 2008 12:12 am

Re: Copying pictures

Post by emendelson »

And I haven't tested this on an M1 Mac yet, and may not be able to do do for a few days. If you're willing to waste some more time on this, perhaps you could try notarizing the application for yourself (you'll need to change the org.wpdos.etc entry in info.plist) and see if that makes it work?
User avatar
Ronald P. Regensburg
Expert User
Posts: 7821
Joined: Thu Feb 09, 2006 10:24 pm
Location: Amsterdam, Netherlands

Re: Copying pictures

Post by Ronald P. Regensburg »

It had possibly something to do with access to the Documents folder. The latest script did not ask to allow access to the Documents folder. Access was checked in System Preferences > Security & Privacy > Files and Folders, possibly because an earlier version did ask for it. I checked and checked again, with no result. I restarted my Mac, with no result. I then installed a fresh instance of the script. This time it asked for access to the Documents folder and preview worked.

This time I ran the script for the first time after I set 'property showImage' to true. The previous instance of the same script was first run with 'property showImage' set to false. After I changed 'property showImage' from false to true, it continued to work without preview. Not sure if that was the determining factor.
User avatar
mabam
Master Emulator
Posts: 497
Joined: Wed Apr 10, 2013 9:32 am

Re: Copying pictures

Post by mabam »

Ronald P. Regensburg wrote: Mon Aug 30, 2021 2:36 pm It had possibly something to do with access to the Documents folder. The latest script did not ask to allow access to the Documents folder. Access was checked in System Preferences > Security & Privacy > Files and Folders, possibly because an earlier version did ask for it. I checked and checked again, with no result. I restarted my Mac, with no result. I then installed a fresh instance of the script. This time it asked for access to the Documents folder and preview worked.
I experienced this behaviour with other apps in the past. After deleting the app from the Security & Privacy pane in System Preferences, upon starting the newer version of the app I was asked for access again and then it worked fine.

Ronald P. Regensburg wrote: Mon Aug 30, 2021 2:36 pm The previous instance of the same script was first run with 'property showImage' set to false. After I changed 'property showImage' from false to true, it continued to work without preview. Not sure if that was the determining factor.
I also ran the app without amending the script first but the preview did appear after changing it to true.
emendelson
Forum All-Star
Posts: 1706
Joined: Tue Oct 14, 2008 12:12 am

Re: Copying pictures

Post by emendelson »

I'm glad this got sorted out. Apple now recommends that AppleScript apps should be run-only, which may avoid some of these complications, so the ZIP file that I uploaded now has two versions of the script: a run-only copy, with the same name as before, and a copy with "editable" in its name which you can modify however you like. If you ever want to make a run-only copy of the editable version, you can do this in Script Editor.
User avatar
Ronald P. Regensburg
Expert User
Posts: 7821
Joined: Thu Feb 09, 2006 10:24 pm
Location: Amsterdam, Netherlands

Re: Copying pictures

Post by Ronald P. Regensburg »

The issue I was having with the script may be related to an issue with System Preferences > Security & Privacy > Privacy > Files and Folders.
Although I unlocked the prefs panel to make changes, in that particular section I cannot select, remove, or add items. Also the "+" button is dead. Alle other Privacy settings are editable, but not Files and Folders. Restarting my machine does not help.

Edit: I searched for a solution, but apparently this is normal behavior. One can only check or uncheck the boxes, but one cannot add or remove apps from the list.
User avatar
mabam
Master Emulator
Posts: 497
Joined: Wed Apr 10, 2013 9:32 am

Re: Copying pictures

Post by mabam »

emendelson wrote: Mon Aug 30, 2021 3:20 pm I'm glad this got sorted out.
As far as Ronald is concerned it is. But changing the app’s name still doesn’t do anything for me.

Both of the apps in your renewed download show the preview now. Setting the script to false in the editable one and starting it with the option key after adding “Key” to its name still doesn’t activate the preview for me (nor does adding “Show” do that).
With
mabam wrote: Mon Aug 30, 2021 3:10 pm I experienced this behaviour with other apps in the past. After deleting the app from the Security & Privacy pane in System Preferences, upon starting the newer version of the app I was asked for access again and then it worked fine.
I didn’t refer to your app. I didn’t sit in front of my computer, it was just a general comment typed on my mobile while doing groceries. (And I should have been at the computer ’cause then I would have realised that your app doesn’t appear in Security & Privacy.) Sorry for not being clear in that quote.

Ronald P. Regensburg wrote: Mon Aug 30, 2021 4:15 pm The issue I was having with the script may be related to an issue with System Preferences > Security & Privacy > Privacy > Files and Folders.
Although I unlocked the prefs panel to make changes, in that particular section I cannot select, remove, or add items. Also the "+" button is dead. Alle other Privacy settings are editable, but not Files and Folders. Restarting my machine does not help.

Edit: I searched for a solution, but apparently this is normal behavior. One can only check or uncheck the boxes, but one cannot add or remove apps from the list.
Files and Folders only shows apps which were first added to Full Disk Access.
User avatar
mabam
Master Emulator
Posts: 497
Joined: Wed Apr 10, 2013 9:32 am

Re: Copying pictures

Post by mabam »

By the way:
I realised the appearing dialogs are often hidden behind other windows. There’s a remedy for that: Instead of

Code: Select all

display dialog …
write

Code: Select all

tell application (path to frontmost application as text) to display dialog …
emendelson
Forum All-Star
Posts: 1706
Joined: Tue Oct 14, 2008 12:12 am

Re: Copying pictures

Post by emendelson »

Ah! I didn't know that! Will fix later today, I hope.
User avatar
Ronald P. Regensburg
Expert User
Posts: 7821
Joined: Thu Feb 09, 2006 10:24 pm
Location: Amsterdam, Netherlands

Re: Copying pictures

Post by Ronald P. Regensburg »

mabam wrote: Mon Aug 30, 2021 4:40 pmFiles and Folders only shows apps which were first added to Full Disk Access.
All apps in Full Disk Access also show up in Files and Folders, but many apps in Files and Folders, the ones that have access to specific folders and volumes, are not in Full Disk Access. I suppose they are added when the user allows access when asked while running the app.
User avatar
Ronald P. Regensburg
Expert User
Posts: 7821
Joined: Thu Feb 09, 2006 10:24 pm
Location: Amsterdam, Netherlands

Re: Copying pictures

Post by Ronald P. Regensburg »

In the info.plist file is a long list of privacy items.

Code: Select all

	<key>NSAppleEventsUsageDescription</key>
	<string>This script needs to control other applications to run.</string>
	<key>NSAppleMusicUsageDescription</key>
	<string>This script needs access to your music to run.</string>
	<key>NSCalendarsUsageDescription</key>
	<string>This script needs access to your calendars to run.</string>
	<key>NSCameraUsageDescription</key>
	<string>This script needs access to your camera to run.</string>
	<key>NSContactsUsageDescription</key>
	<string>This script needs access to your contacts to run.</string>
	<key>NSHomeKitUsageDescription</key>
	<string>This script needs access to your HomeKit Home to run.</string>
	<key>NSMicrophoneUsageDescription</key>
	<string>This script needs access to your microphone to run.</string>
	<key>NSPhotoLibraryUsageDescription</key>
	<string>This script needs access to your photos to run.</string>
	<key>NSRemindersUsageDescription</key>
	<string>This script needs access to your reminders to run.</string>
	<key>NSSiriUsageDescription</key>
	<string>This script needs access to Siri to run.</string>
	<key>NSSystemAdministrationUsageDescription</key>
	<string>This script needs access to administer this system to run.</string>
I suppose only the first one is needed.
emendelson
Forum All-Star
Posts: 1706
Joined: Tue Oct 14, 2008 12:12 am

Re: Copying pictures

Post by emendelson »

mabam wrote: Mon Aug 30, 2021 4:40 pm Both of the apps in your renewed download show the preview now. Setting the script to false in the editable one and starting it with the option key after adding “Key” to its name still doesn’t activate the preview for me (nor does adding “Show” do that).
I'll have to try to sort this out later this week or early next, as I'll be traveling starting tomorrow. I wonder if macOS is remembering something and not letting the app change its behavior? Maybe we need two separate apps, with different strings in the plist? That would be space-wasting, but it's more likely to work.
Post Reply