The second launchd job ticket format is almost correct. Only a set of quotes need removing.
Quotes
The quotes are no longer needed in the second launchd job ticket, so change:
<string>"set volume output volume 0"</string>
…to…
<string>set volume output volume 0</string>
Grouping Arguments
In this case, the quotes grouped the set of arguments for osascript
to see as one item. Thus osascript
gets two arguments, the flag -e
and the script. This meets the requirements from the osascript manual.
When called through Terminal.app and thus through the shell, the quotes are needed to perform the grouping. When osascript
gets the arguments, the shell has removed the quotes.
Without quotes, spaces subdivide each item passed via the shell.
When called through a launchd job ticket, the grouping is intrinsic within the array passed as ProgramArguments
. In your first ticket, the array contained nine items and in the second ticket, the array contained three.
LaunchAgent
Be sure to add this launchd job as a ~/Library/LaunchAgents
as osascript
requires an active user with a graphical session.
This approach approximates what I do with Power Manager in How to Set the Volume When Logging In.
I’ve got it. I think. Took me a long time but I think I have worked out what’s happening. Hopefully this might be helpful to someone else.
First of all I noticed locking the file in the Finder seemed to stop Preview remembering a different page. So if I opened the PDF on page 1 and then scrolled to page 10 it wouldn’t remember the PDF was on page 10 anymore when the file was closed then reopened. That was the first clue.
I then used fseventer which monitors file changes in real time and I was surprised to see that opening a PDF in Preview, scrolling to a different page and then closing the PDF updated the file itself. It doesn’t literally register a time change in the Finder, the date stays the same, but fseventer threw up the clue ‘Modified xattr’.
Ok, I dimly remembered some mention of this feature called extended attributes from years back, it seems to function like yet another kind of metadata but that’s quite separate from ‘normal metadata’, and now I'm reading was a replacement for the old resource forks in OS 9. Running $mdls
(used for normal 'metadata') in Terminal won’t show extended attributes.
You have to use the command $xattr
to see these attributes.
When I ran $xattr /Volumes/mydisk/mypdf.pdf
I got this:
/Volumes/mydisk/mypdf.pdf: com.apple.Preview.UIstate.v1:
And another line about a quarantine bit
You have to do $ xattr -lv
to see the full data of these extended attributes. It produces a blob of hexadecimal output for com.apple.Preview.UIstate.v1.
So I ran $ xattr -lv /Volumes/mydisk/mypdf.pdf
And copied the output. Then I opened the PDF in Preview, changed the page and closed Preview.
Then I ran $ xattr -lv /Volumes/mydisk/mypdf.pdf
again
And then compared the blobs of hexadecimal output for com.apple.Preview.UIstate.v1, which were different between the two occasions.
The final test was to delete the extended attribute data:
$ xattr -c /Volumes/mydisk/mypdf.pdf
And that’s it! It’s back to page 1 when you open it in Preview
It appears the last page viewed of a PDF is stored in these extended attributes. I’m so happy!!
Best Answer
I don't know in which file the value is stored, I haven't found any file change in your suggested file when I changed the input volume.
But you can access the value via Terminal and AppleScript:
This will get your input volume as a number between 0 and 100.