Can a Mac be programmed to simulate pressing a key at or immediately after the launch of an app or opening of a type file?

I am trying to set the Preview app to not jump from page to page, but scroll between them and only show one column of pages, but I found that the relevant settings won't actually be stored, and will reset after a restart of the app or on the opening of the next file.

EDIT: although there is dedicated function for setting the default one or two page and scroll or non-scroll settings, however, for over 6 years, Preview is buggy, and won't be able to store it. After restarting the app, and opening a .pdf for the very first time it will still open it in 2-page, non-scroll setting.

To find the dedicated (non-operational) function:

Go to Preview > Preferences... > PDF > Opening for the first time: Show as > Continuous Scroll.

This is supposed to set the app to open with one-page continuous scroll even after the app is closed; however, it will NOT store this setting.


Solution 1:

From comments:

user3439894 — It seems like this would need scripting; I can't code much. Any chance you could share your method

Here are the two functions I use in my init.lua file for Hammerspoon to always set Continuous Scroll in Preview, especially in Full Screen view:

function applicationPreviewWatcher(appName, eventType, appObject)
    if (eventType == hs.application.watcher.activated) then
        if (appName == "Preview") then
            appObject:selectMenuItem({"View", "Continuous Scroll"})
        end
    end
end
appPreviewWatcher = hs.application.watcher.new(applicationPreviewWatcher)
appPreviewWatcher:start()
-- appPreviewWatcher:stop()

function spaceHasChangedForPreview()
    local asCommand = "tell application \"System Events\" to return name of process 1 whose frontmost is true"
    local ok, theFrontmostAppName = hs.osascript.applescript(asCommand)
    if ok then
        if (theFrontmostAppName == "Preview") then
            hs.eventtap.keyStroke({"cmd"}, "1")
        end
    end
end 
spaceWatcherForPreview = hs.spaces.watcher.new(spaceHasChangedForPreview)
spaceWatcherForPreview:start()
-- spaceWatcherForPreview:stop()

Notes:

The example Lua code and functions used with Hammerspoon along with the example AppleScript code, shown above, was tested under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and works for me without issue1.

  • 1 Assumes necessary and appropriate setting in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.

The first function always clicks the Continuous Scroll menu item on the View menu whenever Preview is activated 2.

  • 2 The application has been given keyboard/mouse focus.

The second function always clicks the Continuous Scroll menu item on the View menu in Preview when the Desktop/Space changes, if Preview went into Full Screen view (or out of it) and Preview is the front most application.

In either case whether or not Continuous Scroll menu item on the View menu in Preview is already checked, this ensures it is.

The second function is necessary because if your screen is wide enough and Continuous Scroll is checked when the window is not in full screen view it automatically changes to Two Pages when going into full screen view. This keeps it set to Continuous Scroll.