Function for finding a certain string in the history

Hey!
I’d like to check if a certain snapshot exists in the history. Anyone with an idea?

You can get a history state via

function getHistoryState(_index?: number, _name?: string): HistoryState {
  return photoshop.action.batchPlay({
    _obj: 'get',
    _target: [
      {
        _ref: 'historyState',
        ...(!_index && !_name ? { _property: 'currentHistoryState' } : {}),
        ...(_index ? { _index } : {}),
        ...(_name ? { _name } : {})
      },
    ],
    _options: {
      dialogOptions: 'dontDisplay',
    },
  }, { synchronousExecution: true })[0]
}

You can get it by index or name (if you know the exact one), or leave the parameters away to get the current state.

If you don’t know the exact name, but want to check for an included string for example, you need some extra steps:

Each history step looks like this:

export interface HistoryState {
  name: string
  itemIndex: number
  count: number
  auto: boolean // false if snapshot
  historyBrushSource: boolean
  currentHistoryState: boolean
  ID: number
}

where the count is the total amount of states/steps in the history.

So you could do something like:

  1. get state at index 0
  2. read out count
  3. loop from index 1 to count to fetch all the other states by index (collect everything in an array)
  4. do some filtering like const matches = historyStates.filter(state => state.name.includes(*string*) && state.auto === false)
1 Like

Thank you Simon. I don’t know typescript and i will have to sort that out. But i get the idea.