0

Hi I miss the modern feature of osx restoring session after reboot so I would like to write a bash script that save the pdf files open in Preview.app and ask if I want to reopen that files in preview.app if between the last 2 script execution the system has rebooted.


I begin with

lsof | grep '^Preview*pdf$' >> listpreview.txt

but I'm not able to determine the appropriate regex to isolate the path, a typical line returned by lsof
is

Preview 4581 My_user txt REG 14,2 3254550 36522430 /arbitrary_path/filename_with_all_sort_of_character_and_spaces.pdf

  1. There are more direct methods than lsof?
  2. How can I save a .txt file (?) with the list of path of pdf files used in preview.app?
  3. How escape all sort of symbol like ß may be (should not) in file/folder name?

Thanks for your answer.

1 Answers1

0
lsof | grep -i "^Preview.*\.pdf" | awk '{print $NF}' >> listpreview.txt
Arjun Mathew Dan
  • 5,240
  • 1
  • 16
  • 27
  • 1
    May be you should filter for pdf files also – Ashish Oct 24 '14 at 17:58
  • 1
    ` lsof | grep -i "^Preview.*pdf$" | awk '{print $NF}' >> listpreview.txt` – Ashish Oct 24 '14 at 18:07
  • well you answered 2 questions out of 3 there. I will get you answer of 1st question for you now – Ashish Oct 24 '14 at 18:14
  • ` ls -l /proc/7857/fd ` where '7875' is a process id which can be found with `ps` command greping for proper PID can help as work around 2 other than lsof – Ashish Oct 24 '14 at 18:17
  • once you have PID you can also use `pfile {pid}` command to get list of files opened by that process – Ashish Oct 24 '14 at 18:23
  • Thanks, I do lots of confusion with regex, but your solution is right for path without spaces (sure, I've been not very specific in question but I have folder with spaces in name and in pdf too), when spaces I get only what comes after last space (as expected). My way is to math from first slash to end of line if start with Preview and ends with pdf.. then to use the path I have to braket the path between ""? – Alexander Beerhoff Oct 25 '14 at 21:16
  • `lsof | grep -i "^Preview.*pdf$" | awk 'match($0,/\/.*/) {print substr($0,RSTART,RLENGTH)}' | sed 's/$/\"/g;s/^/\"/g' >> my_listofpath` create the right list of path but when I try to open a path, the last for example, thing are messed up: the command open -a Preview `awk '/./{line=$0} END{print line}' list33` returns an error message showing a list of path with the spaced names broken and mixed. – Alexander Beerhoff Oct 26 '14 at 16:58