How to support this blog?

To support this blog, you can hire me as an OmegaT consultant/trainer, or you can send translation and project management jobs my way.

Search the site:

Showing posts with label Command line. Show all posts
Showing posts with label Command line. Show all posts

Restore hidden files from Time Machine

About backups

A working backup is like a stock of 10 billion masks waiting for a pandemic. You only use it when things go bad and since you never know when things go bad your backup needs to work flawlessly all the time.

Time Machine makes it extremely easy to restore anything that's on the backup disk to your machine. If you have not yet installed an external drive for your Time Machine backups, do it NOW. There is nothing more important than having a reliable backup of all your important files.

Time Machine has an optional setting (System Preferences > Time Machine > [Options...] button) where you decide what not to backup. Anything that is not there is basically copied to the Time Machine disk.

Hidden files are also hidden in Time Machine

This is not a Time Machine tutorial, so I'll stop here. But, what matters is that when you click on the "Enter Time Machine" menu item, macos shows a nice time based display of the contents of the current window, but it only shows what is visible. Not hidden files that are used in Unix applications as preference files and that are routinely created at the root of your home folder.

Yesterday I messed with one such file and since it was hidden, I could not restore it from the Time Machine display.

The easy way, for moderately experienced people

Time Machine can be accessed from the command line with the tmutil command.

$ man tmutil

TMUTIL(8)                 BSD System Manager's Manual                TMUTIL(8)

NAME
     tmutil -- Time Machine utility

SYNOPSIS
     tmutil verb [options]

DESCRIPTION
     tmutil provides methods of controlling and interacting with Time Machine,
     as well as examining and manipulating Time Machine backups.
     Common abilities include restoring data from backups, editing exclusions,
     and comparing backups.

The "verb" we're looking for is restore and its syntax is:

restore [-v] src ... dst
        Restore the item src, which is inside a snapshot, to the location dst.
        The dst argument mimics the destination path semantics of the cp tool.
        You may provide multiple source paths to restore. The last path
        argument must be a destination.

The src is where the restore date/time can be chosen since the backups have their own folders based on date/time.

So what I needed was:

$ tmutil restore /Time/Machine/path/to/.hidden.file ~/.hidden.file.test

I restored to a .test file just to make sure that really was the file I wanted.

The longer road, a half command-line, half GUI approach

Another solution is to make hidden files visible in Finder, start Time Machine, restore from the graphical interface and re-hide the files (they're hidden to avoid messing with them by accident).

There is no visible Finder setting that allows for that, so to get the job done one still has to use the command line to modify some non-GUI Finder setting from Terminal. This has been documented for years but I keep forgetting about it.

In Terminal, enter the following command:

$ defaults write com.apple.finder AppleShowAllFiles TRUE

What does all that mean ?

In Terminal, enter the following comment:

$ man defaults

To display the "defaults" command "man"ual. You should see:

DEFAULTS(1)               BSD General Commands Manual              DEFAULTS(1)

NAME
    defaults -- access the Mac OS X user defaults system

SYNOPSIS
    defaults [-currentHost | -host hostname] read [domain [key]]
    defaults [-currentHost | -host hostname] read-type domain key
    defaults [-currentHost | -host hostname] write domain { 'plist' | key 'value' }
    defaults [-currentHost | -host hostname] rename domain old_key new_key
    defaults [-currentHost | -host hostname] delete [domain [key]]
    defaults [-currentHost | -host hostname] { domains | find word | help }

(etc.)

Do you see the "defaults [-currentHost | -host hostname] write domain { 'plist' | key 'value' }" line ?

What that means is that the command syntax is:

defaults + optionally either "-currentHost" or "-host hostname" + write + the "domain" to which belongs the preference + a compulsory settings value.

In our case, the "domain" is "com.apple.finder", the "key" is "AppleShowAllFiles" and the "value" is "TRUE".

which means "Please, write down somewhere that Finder is now required to show all the files."

Next step

When you're done with your un-hiding hidden files, you can enter Time Machine, restore the file you want and then re-hide the hidden files with:

$ defaults write com.apple.finder AppleShowAllFiles FALSE

Et voilà !

Now, Finder doesn't know about that change of settings until you actually relaunch it. You can do that from Finder itself, or, since you're already in Terminal, you can do it from there by using the following command:

$ killall Finder

where "man killall" gives you:

KILLALL(1)                BSD General Commands Manual               KILLALL(1)

NAME
    killall -- kill processes by name
...

$ killall Finder

will actually kill and restart Finder.

To do the un-hidding and killalling in one fell swoop just tell Terminal that the two commands should be followed in order:

$ defaults write com.apple.finder AppleShowAllFiles TRUE; killall Finder

And the same for hidding them again when you're done with Time Machine:

$ defaults write com.apple.finder AppleShowAllFiles FALSE; killall Finder

Knowing about the command line is a must

Knowing about the command line and Terminal in a must. You can't pretend that system does not exist and trying to make sense of all that will drastically increase the amount of stuff you can do on your machine. There are plenty of tutorials on the web. One day I'll eventually write one for "us" translators, with commands that are relevant to our work...

From Finder to Terminal: cd anywhere

At a point in a Mac user life, the Terminal utility and all the command line applications that it brings to the game become a daily necessity. Typically, you work in a Finder window and want to work on the window items from the command line. For that, you can go up to the parent window, select the folder that you just exited and copy it, move to terminal, hit "cd", add a space, paste the folder and hit "Enter". That's a lot of fiddling around.


I just checked and found that I needed about 10 seconds to complete the task. 10 seconds, 4 times a day, 5 times a week, 40 times a year and you've lost 8,000 seconds = more than 2 hours. Eventually you'll notice that you're wasting time on this particular task and you end up either cursing yourself that you can't go faster, or start looking for a solution.
Here is my take on the problem.
On a side note, I spent way more than 2 hours to find that solution (and all the solutions that I write about here). Namely, I had to learn enough AppleScript, then I applied that knowledge to Finder and Terminal, then I found all the issues that made this a non trivial task, then I asked around (eternal thanks to the members of the AppleScript Users list hosted by Apple), then I tried a new implementation, then I was not satisfied with it, rewrote the whole thing and that's where I stand today. But the time spent on learning AppleScript and the workings of the various applications translates into knowledge (along with its own lot of frustration) that I can apply to other issues, while not spending that time only translates in frustration and a sense that you can't do much with computers...




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

tell application "Finder"
activate
try # if what is selected in Finder is a folder, then use that folder
if class of item 1 of (selection as list) is folder then
set myFolder to item 1 of (selection as alias list)
else # if nothing is selected or if the selection is not a folder, use the parent folder
set myFolder to insertion location as alias
end if
on error # if nothing works, default to using the Desktop
set myFolder to desktop as alias
end try
set myPath to quoted form of (POSIX path of myFolder)
set myCommand to "cd " & myPath
end tell

# I use a separate "handler" to launch the command.
# That handler can be saved in a script library so as to be able to call it from other scripts

my launchMyCommand(myCommand)

# A handler is useful because what matters is what it outputs, not how it works.
# So, if at one point in the future I decide that this way of launching a command in Terminal is not efficient anymore, I can change the way the handler works but I won't have to change the scripts that call it.

on launchMyCommand(myCommand)
# First I use GUI scripting to create a new Terminal window
tell application "Terminal" to activate
tell application "System Events" to tell application process "Terminal"
set frontmost to true
delay 0.1
keystroke "n" using {command down}
end tell
# Then I ask the newly created window to run the command
tell application "Terminal"'s front window
delay 0.1
do script myCommand in its last tab
activate
end tell
# And I eventually merge that window to the other so as to keep everything tidy
tell application "System Events" to tell application process "Terminal"
set frontmost to true
delay 0.1
keystroke "m" using {control down, command down}
end tell
return

end launchMyCommand

I save this script as an AppleScript application and I call it ">cd". Now when I am working in Finder, I just call Spotlight with a system shortcut, I start hitting >c and Spotlight autocompletion proposes >cd.app, I hit Enter and I'm in Terminal with a window opened on the item I wanted.

Bash (command line) shortcuts

Until today, I only used the following shortcuts on the command line:

  • ctrl+a → go to beginning of line
  • ctrl+e → go to end of line
  • ctrl+k → delete to end of line

And even though I was very frustrated by the fact that I had to use the mouse to select/copy/paste etc, it never really occured to me to google for "bash command line shortcuts"...

To make it short: bash uses the readline library.

The user documentation for readline is at:
http://cnswww.cns.cwru.edu/php/chet/readline/rluserman.html

All the shortcuts that exist on your machine can be found by using the 'bind' command:
$ bind -P | more

You'll have more shortcuts with:
$ bind -p | more

If you want to know how to use 'bind' to add shortcuts, check its help file:
$ help bind

Select/cut/paste require to first bind a shortcut to "kill-region":
$ bind "\C-xx": kill-region
(ctrl+x followed by x)

Now we need to select the string (create a "region").

A region is automatically created between a "mark" and the cursor. So, we only need to put the mark at one end of the region and to move the cursor to the other end.

To create a mark use: ctrl+@ or ctrl+space, then move the cursor, then hit ctrl+xx to "kill" the region.

Pasting requires ctrl+y.

But, the ctrl-y (Yank/paste) command works only within the application from where the string was "killed". The "kill-ring" is not shared by other applications (even though they may accept ctrl-y as a Yank command).

So, if you want to get a string from terminal and paste it outside (like select a command and paste it into a mail for ex), here is something that may not be the most efficient way to go, but it works:
echo string | pbcopy
or
echo "complex string" | pbcopy

For ex, to put the above line in the pastebpard, I'd do:
echo "echo \"complex string\" | pbcopy" | pbcopy

Okapi tools on OSX...

In my "Okapi tools for Mono" post, last November, I discussed the possibility to use Okapi on OSX without installing Parallels/VMWare/WhateverVirtualSolutionYouPrefer and Windows and .NET... Which in the end makes Okapi a very expensive free tool...

Okapi has recently been released to work on Mono, the free (as a bird) version of .NET and I've promised myself to install everything and try the package as soon as I'd have a job requiring it.

This job has come. At last. It is a small set of InDesign files (that will eventually end up being Illustrator files) that are part of a much bigger translation package...

So, at the time of writing, here is the procedure you need to follow to get everything to work:

(Beware, Okapi for mono does not come with a graphical interface and everything must be done in the Terminal, at the command line. It is not terribly hard but not super trivial either for the translator who is not used to that. For people who need an introduction to working with the Terminal on OSX, check this link.)


  1. Download Mono (1.2.6 at the time of the post):

    http://www.go-mono.com/mono-downloads/download.html

  2. Install it by clicking on the package file and following the instructions.

  3. Download Okapi for Mono (R00020 at the time of the post):

    http://sourceforge.net/project/showfiles.php?group_id=42949

  4. Unzip/untar the package and read the ReadMe_ForMono.htm file found in the Tikal folder

  5. Do what the file says under "Using the Okapi tools"

    (for some reason, I am yet unable to properly set the path to Okapi...)



When this is done, you'll need to take a look at the Mono for OSX page to know how to use the beast.

Basically, it comes down to opening the Terminal and typing:

$ mono ~/bin/OkapiMono/Tikal/Tikal.exe

Here, I've left my path to Tikal because I could not set it properly in the install step. But a proper install should accept:

$ mono Tikal.exe

Running only Tikal.exe without any arguments will have the application display its basic help where you'll see what you need to type to do a few basic things, like:

$ mono Tikal.exe -lf
to list the available filters.

Just for your information, since you can find that on the Okapi help pages too, here are the filters that are directly available from the Mono command line:

  • okf_po (Okapi PO Filter)

  • okf_properties (Okapi Properties Filter)

  • okf_script (Okapi Script Filter)

  • okf_netres (Okapi NETRes Filter)

  • okf_rc (Okapi RC Filter)

  • okf_xml (Okapi XML Filter)

  • okf_wordfast (Okapi Wordfast Filter)

  • okf_tradostext (Okapi Trados Text Filter)

  • okf_illustrator (Okapi Illustrator Filter)

  • okf_table (Okapi Table Filter)

  • okf_inx (Okapi INX Filter)

  • okf_html (Okapi HTML Filter (ALPHA TEST ONLY)
  • )
  • okf_json (Okapi JSON Filter)



If you use the parameter -lu you'll get a list of available utilities:

  • oku_set01:extraction

  • oku_set01:merging

  • oku_set01:rewriting

  • oku_set01:encodingconversion

  • oku_set01:lbconversion

  • oku_set01:bomconversion

  • oku_set02:qualitycheck

  • oku_set02:update

  • oku_set02:rtfconversion

  • oku_set02:xsltransformation

  • oku_set02:twbanalysis

  • oku_set02:rtfsplitting

  • oku_set02:alignment

  • oku_set02:rtftotmx

  • oku_set03:alignment

  • oku_set04:dnllistedit

  • oku_set04:xliffsplitting

  • oku_set04:xliffconcatenation

  • oku_set04:tmxsplittingdup

  • oku_set04:xliffconversion

  • oku_set04:proofreading

  • oku_set04:searchandreplace

  • oku_set04:uriconversion

  • oku_set04:cdataconversion

  • oku_set04:xmlpruning

  • oku_set04:ttx2tmx

  • oku_set04:xmlpartitioning


All the filters and utilities etc require specific parameters to run. Check the Okapi for Mono manual that comes with the download to know how to use all that.

Don't forget that Okapi has a very nice user group managed by Yves Savourel, the developer. It is possible to access the archives without having to subscribe to it.

Et voilà ! That is pretty much everything you need to know to get started... I hope that helped !

Popular, if not outdated, posts...

.docx .NET .pptx .sdf .xlsx AASync accented letters Accessibility Accessibility Inspector Alan Kay alignment Apple AppleScript ApplescriptObjC AppleTrans applications Aquamacs Arabic archive Automator backup bash BBEdit Better Call Saul bug Butler C Calculator Calendar Chinese Cocoa Command line CSV CSVConverter database defaults Devon Dictionary DITA DocBook Dock Doxygen EDICT Emacs emacs lisp ergonomics Excel external disk file formats file system File2XLIFF4j Finder Fink Font français Free software FSF Fun Get A Mac git GNU GPL Guido Van Rossum Heartsome Homebrew HTML IceCat Illustrator InDesign input system ITS iWork Japanese Java Java Properties Viewer Java Web Start json keybindings keyboard Keynote killall launchd LISA lisp locale4j localisation MacPorts Mail markdown MARTIF to TBX Converter Maxprograms Mono MS Office NeoOffice Numbers OASIS Ocelot ODF Okapi OLPC OLT OmegaT OnMyCommand oo2po OOXML Open Solaris OpenDocument OpenOffice.org OpenWordFast org-mode OSX Pages PDF PDFPen PlainCalc PO Preview programming python QA Quick Look QuickSilver QuickTime Player Rainbow RAM reggy regular expressions review rsync RTFCleaner Safari Santa Claus scanner Script Debugger Script Editor scripting scripting additions sdf2txt security Services shell shortcuts Skim sleep Smultron Snow Leopard Spaces Spanish spellchecking Spotlight SRX standards StarOffice Stingray Study SubEthaEdit Swordfish System Events System Preferences Tags TBX TBXMaker Terminal text editing TextEdit TextMate TextWrangler The Tool Kit Time Capsule Time Machine tmutil TMX TMX Editor TMXValidator transifex Translate Toolkit translation Transmug troubleshooting TS TTX TXML UI Browser UI scripting Unix VBA vi Virtaal VirtualBox VLC W3C web WebKit WHATWG Windows Wine Word WordFast wordpress writing Xcode XLIFF xml XO xslt YAML ZFS Zip Zotero